0

This is the json response from my server . How can i parse it and store it in HashMap? Please help me.

 {'records':
     [{
        'number':165, 
        'description': 'abcd'
      },
      {
         'number':166, 
         'description': 'ab'
      },
      {
         'number':167, 
         'description': 'abc'
      }]
  }
Pratik
  • 30,639
  • 18
  • 84
  • 159
Santhosh
  • 4,956
  • 12
  • 62
  • 90
  • The programming language you're using may provide a JSON parser as standard library functionality, but since you're not disclosing which language you're using, it's hard to help you. – hmakholm left over Monica Aug 26 '11 at 11:58
  • 5
    17 questions asked and not a single answer accepted? Really?? Please fix this before asking more questions. – Amir Raminfar Aug 26 '11 at 11:59
  • possible duplicate of [Sending and Parsing JSON in Android](http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android) – a'r Aug 26 '11 at 12:08

4 Answers4

1

im new in android but maybe you can do something like this:

JSONObject JsonObject = new JSONObject(json);
JSONArray JsonArray_ = JsonObject .getJSONArray("records");

for (int i = 0; i < numberOfItems; i++) {
JSONObject record= JsonArray_photo.getJSONObject(i);    
parsedObject.number = record.getString("number"); //its the same for all fields        
map.add(parsedObject);
}

I done something like that for my own JSON parser. hope this helps. cheers

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
Veljko
  • 1,893
  • 6
  • 28
  • 58
0

I suggest you look at the gson library, which makes it very easy indeed to parse JSON into an object representing the data. So you could create a Record object, with public member variables for number, description, and then use gson to parse the JSON into an object array.

http://code.google.com/p/google-gson/

Add the .jar to your libs folder and then use it like this:

Record[] records = new GsonBuilder().create().fromJson(jsonString,Record[].class)
int number = record[0].number;
Ollie C
  • 28,313
  • 34
  • 134
  • 217
0

The org.json package is included in Android: http://developer.android.com/reference/org/json/package-summary.html

Use is simple:

JSONObject json_object = new JSONObject(String json_string);

Each property can then be accessed as a Java property, e.g. json_object.property. It will throw a JSONException if the parsing fails for some reason (i.e., invalid JSON).

Paddy
  • 2,793
  • 1
  • 22
  • 27
0

You can use Gson library, you can find full tutorial on softwarepassion.com

Kris
  • 5,714
  • 2
  • 27
  • 47