1

I have the following output, where wsResponse.get("result").toString() = {"status":1,"result":{"2011":102003,"2010":100003,"2009":98723,"2008":129023}}

I'll like to create a hashmap where the years are the key.

How will i be able to populate the hashmap?

Zachary
  • 15
  • 4
  • You might start here. Hope it helps! http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java – loeschg Mar 14 '12 at 15:02

2 Answers2

1

Is it necessary to have a hashmap? Android has the org.json library, so you can do something like:

JSONObject json = new JSONObject(wsResponse.get("result").toString());

If you check out the reference, you can use JSONObject just like a hashmap. You can call get(String key) to get the value for a year.

If you must have a hashmap, you can iterate through the JSONObject and put the keys and values in the hashmap.

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • Yeah, this might help, too. http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java – loeschg Mar 14 '12 at 15:03
  • I've got the json object: JSONObject json = new JSONObject(); json = (JSONObject)wsResponse.get("result"); But didn't quite manage to iterate through the key value pairs. Is there an iterator for JSON like that of hashmap? – Zachary Mar 14 '12 at 16:40
  • Yes. json.keys() will return an iterator for the key names. – Samuel Mar 14 '12 at 21:19
0

Take a look at JSONObject.

neevek
  • 11,760
  • 8
  • 55
  • 73