所以我有以下Json文件:
["burger" : [{"Name": "Chicken Burger", "Price": 3.00},{"Name": "Cheeseburger", "Price": 2.00},{"Name": "beef burger", "Price": 2.00}]"burgerToppings" : [{"Name": "Lettuce"},{"Name": "Tomato"},{"Name":"Cucumber"},]"burgerSauces" : [{"Name": "Mayo"},{"Name": "ketchup"},{"Name": "Spicey mayo"}]]
我想将“汉堡”的名称和价格存储在单独的ArrayList中。
一个后续问题是我希望能够将新值/元素添加到JSONArray
更新:这是在Java中,eclipse
答案 0 :(得分:0)
您将不得不使用该模式来修复一些缺少的属性,例如burgerSausce等。尽管您有所了解
看起来您的JSON格式错误。我相信您正在寻找的是:
{"burger" :
{ "burgerTypes" :
[
{"Name": "Chicken Burger", "Price": 3.00},
{"Name": "Cheeseburger", "Price": 2.00},
{"Name": "beef burger", "Price": 2.00}
]
},
"burgerToppings" : [
{"Name": "Lettuce"},
{"Name": "Tomato"},
{"Name":"Cucumber"}
],
"burgerSauces" : [
{"Name": "Mayo"},
{"Name": "ketchup"},
{"Name": "Spicey mayo"}
]
}
我将采用该JSON,将其转换为模式,然后通过转换器运行它以创建一些POJO。生成POJO后,请使用Jackson来完成工作。
我去了http://www.jsonschema2pojo.org/并生成了以下内容:
-----------------------------------com.example.Burger.java-----------------------------------
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"burgerTypes"
})
public class Burger {
@JsonProperty("burgerTypes")
public List<BurgerType> burgerTypes = null;
}
-----------------------------------com.example.BurgerSauce.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name"
})
public class BurgerSauce {
@JsonProperty("Name")
public String name;
}
-----------------------------------com.example.BurgerTopping.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name"
})
public class BurgerTopping {
@JsonProperty("Name")
public String name;
}
-----------------------------------com.example.BurgerType.java-----------------------------------
package com.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name",
"Price"
})
public class BurgerType {
@JsonProperty("Name")
public String name;
@JsonProperty("Price")
public Float price;
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"burger",
"burgerToppings",
"burgerSauces"
})
public class Example {
@JsonProperty("burger")
public Burger burger;
@JsonProperty("burgerToppings")
public List<BurgerTopping> burgerToppings = null;
@JsonProperty("burgerSauces")
public List<BurgerSauce> burgerSauces = null;
}
现在有了实体,您可以简单地致电:
Burger burger = objectMapper.readValue(json, Burger.class);
答案 1 :(得分:0)
您可以使用here轻松完成此操作(一旦您修复了JSON格式)
[{"burger" : [{"Name": "Chicken Burger", "Price": 3.00},{"Name": "Cheeseburger", "Price": 2.00},{"Name": "beef burger", "Price": 2.00}],"burgerToppings" : [{"Name": "Lettuce"},{"Name": "Tomato"},{"Name":"Cucumber"}],"burgerSauces" : [{"Name": "Mayo"},{"Name": "ketchup"},{"Name": "Spicey mayo"}]}]
$[0].burger..Name
[
"Chicken Burger",
"Cheeseburger",
"beef burger"
]
还有
$[0].burger..Price
[
3.0,
2.0,
2.0
]