【发布时间】: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>();
【问题讨论】:
-
试试 Jackson 映射器github.com/FasterXML/jackson-databind
标签: java arrays json arraylist