这里有什么错误?我无法将其解析到列表中。
我正在尝试使用jsonUrl从此文件中获取json。
请让我知道我在哪里犯这个错误。
"recommended":[
{
"thumbnail":"http://farm8.staticflickr.com/7390/11919320035_1f6dd4da79_z.jpg",
"itemname":"Chilli Babycorn",
"itemtype":"veg",
"price":"45"
},
{
"thumbnail":"http://res.cloudinary.com/dhdglilcj/image/upload/v1455448132/foodonz/dishes/d7.jpg",
"itemname":"Honey Chilli Potato",
"itemtype":"veg",
"price":"90"
}
],
"veg starters":[
{
"itemname":"Paneer Tikka",
"itemtype":"veg",
"price":"110"
},
{
"itemname":"Aloo Tandoori",
"itemtype":"veg",
"price":"60"
}
]
}
这是itemsMenus类
请将其与上述代码联系起来。
public class ItemsMenu {
private String thumbnail;
private String itemname;
private String price;
public String getItemtype() {
return itemtype;
}
public void setItemtype(String itemtype) {
this.itemtype = itemtype;
}
private String itemtype;
private String quantity="0";
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getItemname() {
return itemname;
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
}
答案 0 :(得分:2)
只需使用http://www.jsonschema2pojo.org/将任何类型的json转换为写入模型。因为你的jsons似乎很简单,并且不需要使用TypeToken。并进一步使用
new Gson().fromJson(jsonString,model.class)
您将获得所需的列表。
答案 1 :(得分:1)
您可以将json
字符串直接转换为List<ItemsMenu>
List<ItemsMenu> list = new Gson().fromJson(jsonString, new TypeToken<List<ItemsMenu>>(){}.getType());