0

This is the result of my servlet in json Array that is converted to string using

 /*Converting Json Array to String*/
         StringWriter toStringF = new StringWriter();
         arrayFood.writeJSONString(toStringF);
         /*Printing Json Array - String*/
         response.getWriter().println(toStringF.toString());

The result shows

[{"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}}] 

I tried to search the net on how to convert the jsonArrayStrings to arraylist of object but there are different kinds of codes and imports and right now I am totally confused which to use. It will help me a lot if there is a simpler way to do this.

I want to convert the above JsonArray Strings to an object.. something like the FoodName can be set into its properties like this one ex. food.FoodName(jsonObject.getString("FoodName"))..

ArrayList<Food> food = new ArrayList<Food>();
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
SCS
  • 1,162
  • 3
  • 17
  • 31

1 Answers1

-1

Refer this 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"));
}
Abhash Upadhyaya
  • 717
  • 14
  • 34