0

I have a JSONObject like this

{"message":{"context":"ws","data":"","id":"12345","http_accept":"json","method":"GET","search_key":"cat"},"response":{"1":"cat", "2":"catte"},"status":"OK","code":200}.

I am trying to get the result of a search webservice. I want the value of the pairs from the "response" key to add them to an ArrayList.

For example, from this "response" I want "cat" and "catte". How can I parse to get them?

Gabi R
  • 5
  • 1
  • 3

2 Answers2

0

"response"s structure is map. You can easily parse it with Gson. example here

Community
  • 1
  • 1
Muzaffer
  • 1,457
  • 1
  • 13
  • 22
0

Use a JSON Iterator to loop over all the keys and collect the response values.

JSONObject root = new JSONObject(jsonInput);
JSONObject resp = root.getJSONObject("response");

List<String> respList = new ArrayList<String>();
for (Iterator<String> iterator = resp.keys(); iterator.hasNext();) {
    String key = iterator.next();
    respList.add(resp.getString(key));
    System.out.println(key + " = " + resp.getString(key));
}
System.out.println(respList);

Output :

1 = cat
2 = catte
[cat, catte]

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89