如何将JSON数组转换为Java ArrayList对象

时间:2015-12-04 07:52:18

标签: java arrays json arraylist

这是我在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转换为对象的arraylist,但是有不同种类的代码和导入,现在我完全糊涂哪个使用。如果有更简单的方法可以帮助我。

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

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

1 个答案:

答案 0 :(得分:-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"));
}