gpt4 book ai didi

java - 将 JsonArray 数据添加到 ArrayList 中

转载 作者:行者123 更新时间:2023-12-02 02:19:13 25 4
gpt4 key购买 nike

如何添加 jsonArray数据转化为 ArrayList产品类型。

我用虚拟 json 数据编写了一个测试类。主要类是如下:

public class convertData {

public static void main(String[] args) {

List<ProductModel> myList = new ArrayList<>();

JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
JsonObject jsonObject1, jsonObject2, jsonObject3;


jsonObject1 = Json.createObjectBuilder()
.add("id", 1)
.add("name", "Albany")
.add( "manufacture", "Albany Superior Low Gi Sliced Brown Seed Bread 700g")
.add("price", 15.49)
.add("category", "Food")
.add ("type", "Breads")
.add( "image", "data:image/jpeg;base64,/9j/4A" )
.build();

jsonObject2 = Json.createObjectBuilder()
.add("id", 1)
.add("name", "Albany")
.add( "manufacture", "yuyyjjgyced Brown Seed Bread 700g")
.add("price", 15.49)
.add("category", "Food")
.add ("type", "Breads")
.add( "image", "data:image/jpeg;base64,/9j/4A" )
.build();

jsonObject3 = Json.createObjectBuilder()
.add("id", 1)
.add("name", "Albany")
.add( "manufacture", "Albany Superior Low Gi Sliced Brown Seed Bread 700g")
.add("price", 15.49)
.add("category", "Food")
.add ("type", "Milk")
.add( "image", "data:image/jpeg;base64,/9j/4A" )
.build();


arrayBuilder.add(jsonObject1);
arrayBuilder.add(jsonObject2);
arrayBuilder.add(jsonObject3);

JSONArray jArray = (JSONArray) arrayBuilder;

if (jArray != null) {

for (int i=0;i<jArray.length();i++){
myList.add(jArray.get(i));
}
}

}
}

然后我创建了一个产品模型类,如下所示:

class ProductModel {

private int id;
private String name;
private String manufacture;
private Double price;
private String category;
private String type;
private String image;
// get and setters
...

我搜索过类似的解决方案,但没有帮助方式。

项目是使用J2SE平台编写的,我只添加了jarjson 文件。

我正在构建的 json 数据看起来像这样:

[
{
"id": 1,
"name": "Albany",
"manufacture": "Albany Superior Low Gi Sliced Brown Seed Bread 700g",
"price": 15.49,
"category": "Food",
"type": "Breads",
"image": "data:image/jpeg;base64,/9j/4..."
},
{
"id": 2,
"name": "Blue Ribbon",
"manufacture": "Blue Ribbon Brown Plus Low Gi Bread 700g",
"price": 13.99,
"category": "Food",
"type": "Breads",
"image": "data:image/jpeg;base64,/9j/4AAQSkZ."
},
{
"id": 3,
"name": "Cheese",
"manufacture": "Galbani Mozzarella Cheese 300g",
"price": 49.99,
"category": "Food",
"type": "Cheese",
"image": "data:image/octet-stream;base64,/9j/4AAQSkZJRg..."
}
]

最佳答案

需要做一些改变才能使其运行:

  1. 在 ProductModel 类中添加一个构造函数,该构造函数将接受 JsonObject 作为参数:

    public ProductModel(JsonObject json) {
    super();
    this.id = json.getInt("id");
    this.name = json.getString("name");
    this.manufacture = json.getString("manufacture");
    this.price = Double.parseDouble(json.getJsonNumber("price").toString());
    this.category = json.getString("category");
    this.type = json.getString("type");
    this.image = json.getString("image");
    }
  2. 使用 JsonArray 而不是 JSONArray 并将构建数组传递给它:

    JsonArray jArray = arrayBuilder.build();
  3. 在add方法中调用新创建的构造函数:

    if (jArray != null) { 

    for (int i=0;i<jArray.size();i++){
    myList.add(new ProductModel((JsonObject) jArray.get(i)));
    }
    }

关于java - 将 JsonArray 数据添加到 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762309/

25 4 0