java - 在 Java 中读取 JSON 数组

标签 java arrays json

我正在尝试用 Java 读取 JSON 文件(我从 JSON 开始)。

JSON 文件:

[
  {
    "idProducto":1,
    "Nombre":"Coca Cola",
    "Precio":0.9,
    "Cantidad":19
  },
  {
    "idProducto":2,
    "Nombre":"Coca Cola Zero",
    "Precio":0.6,
    "Cantidad":19
  },
[....]
]

我尝试了以下方法:

ArrayList<Dispensador> Productos = new ArrayList<Dispensador>();

    FileReader reader = new FileReader(new File("productos.json"));
    JSONParser jsonParser = new JSONParser();
    JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
    JSONObject object = (JSONObject) jsonArray.get(0);
    Long idProducto = (Long) object.get("idProducto");
    JSONArray nombres = object.getJSONArray("idProducto");

    Iterator i = jsonArray.iterator();

    while (i.hasNext()) {
        String nombre = (String) object.get("Nombre");
        Double precio = (Double) object.get("Precio");
        BigDecimal precioB = new BigDecimal(precio);
        Long cantidad = (Long) object.get("Cantidad");
        int cantidadB = toIntExact(cantidad);
        System.out.println(nombre);
        Productos.add(new Dispensador(nombre, precioB, cantidadB));
    }

但是进入循环。我也尝试使用 for 循环,但没有成功。

谢谢!

最佳答案

你可以使用gson

您可以使用 Maven 或 jar 文件:http://mvnrepository.com/artifact/com.google.code.gson/gson

package com.test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class AppJsonTest {

    public static void main(String[] args) {
        List<DataObject> objList = new ArrayList<DataObject>();
        objList.add(new DataObject(1, "Coca Cola", 0.9, 19));
        objList.add(new DataObject(2, "Coca Cola Zero", 0.6, 19));

        // Convert the object to a JSON string
        String json = new Gson().toJson(objList);
        System.out.println(json);

        // Now convert the JSON string back to your java object
        Type type = new TypeToken<List<DataObject>>() {
        }.getType();
        List<DataObject> inpList = new Gson().fromJson(json, type);
        for (int i = 0; i < inpList.size(); i++) {
            DataObject x = inpList.get(i);
            System.out.println(x.toString());
        }
    }
}

class DataObject {
    int idProducto;
    String Nombre;
    Double Precio;
    int Cantidad;

    public DataObject(int idProducto, String nombre, Double precio, int cantidad) {
        this.idProducto = idProducto;
        Nombre = nombre;
        Precio = precio;
        Cantidad = cantidad;
    }

    public int getIdProducto() {
        return idProducto;
    }

    public void setIdProducto(int idProducto) {
        this.idProducto = idProducto;
    }

    public String getNombre() {
        return Nombre;
    }

    public void setNombre(String nombre) {
        Nombre = nombre;
    }

    public Double getPrecio() {
        return Precio;
    }

    public void setPrecio(Double precio) {
        Precio = precio;
    }

    public int getCantidad() {
        return Cantidad;
    }

    public void setCantidad(int cantidad) {
        Cantidad = cantidad;
    }

    @Override
    public String toString() {
        return "DataObject [idProducto=" + idProducto + ", Nombre=" + Nombre + ", Precio=" + Precio + ", Cantidad=" + Cantidad + "]";
    }

}

关于java - 在 Java 中读取 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33485769/

相关文章:

java - 在 Android 上使用 root 调用 'ls' 不会返回任何内容,应用程序挂起

java - 为什么 imageView 没有居中?

javascript - 将多个相似的功能合并为一个

json - 如何在名称为 'a%' 的 couchdb 中编写通配符搜索查询

python - 将 Python 字典转换为 JSON 数组

javascript - 使用 jquery 创建基于 JSON 的动态 SelectBox

java - JSON异常 : A JSONObject text must begin with '{' at character

java.lang.错误 : Probable fatal error:No fonts found

C++:动态创建以for循环迭代器命名的数组

java - 如何使用 Java 在 MongoDB 中通过嵌入数组过滤文档