【问题标题】:How to convert JSON Array to Java ArrayList object [duplicate]如何将 JSON Array 转换为 Java ArrayList 对象 [重复]
【发布时间】:2015-12-04 07:52:18
【问题描述】:

这是我在 json 数组中的 servlet 使用

转换为字符串的结果
 /*Converting Json Array to String*/
         StringWriter toStringF = new StringWriter();
         arrayFood.writeJSONString(toStringF);
         /*Printing Json Array - String*/
         response.getWriter().println(toStringF.toString());

结果显示

[{"Food":{"foodName":"Chicken Tenders","foodDescription":"Deep fried chicken tenders with rice","price":150.0,"foodID":59,"rating":5.0}},{"Food":{"foodName":"Roasted Duck\/Asado Rice","foodDescription":"Roasted duck with asado rice","price":210.0,"foodID":32,"rating":4.0}},{"Food":{"foodName":"Steamed Minced Beef Rice","foodDescription":"Steamed minced beef with rice","price":140.0,"foodID":40,"rating":4.0}},{"Food":{"foodName":"Buffalo Wings","foodDescription":"Deep fried chicken wings coated in hot sauce","price":160.0,"foodID":54,"rating":4.0}},{"Food":{"foodName":"H Burger","foodDescription":"The Hatchery's specialty burger","price":135.0,"foodID":58,"rating":4.0}}] 

我试图在网上搜索如何将 jsonArrayStrings 转换为对象的数组列表,但是有不同类型的代码和导入,现在我完全不知道该使用哪个。如果有更简单的方法来做到这一点,它将对我有很大帮助。

我想将上面的 JsonArray 字符串转换为一个对象。像 FoodName 这样的东西可以设置到它的属性中,比如这个 ex。 food.FoodName(jsonObject.getString("FoodName"))..

ArrayList<Food> food = new ArrayList<Food>();

【问题讨论】:

标签: java arrays json arraylist


【解决方案1】:

参考这个site

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
    list.add(arr.getJSONObject(i).getString("name"));
}

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2019-05-17
    • 2014-09-23
    • 1970-01-01
    • 2017-09-19
    • 2018-04-29
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多