user1722323
user1722323

Reputation: 25

Android: JSON Parsing

I have following json generated by my website. I'm completely new with android programming. If possible tell me how to parse following json data to display in listview.

JSON:

  {
   "Category":[
      {
         "id":"14",
         "parent_id":"2",
         "lft":"3",
         "rght":"18",
         "name":"Category Name",
         "icon":"",
         "created":"2012-04-21 23:43:37",
         "modified":"2012-04-21 23:43:37"
      },
      {
         "id":"15",
         "parent_id":"2",
         "lft":"19",
         "rght":"28",
         "name":"Category Name",
         "icon":"",
         "created":"2012-04-21 23:44:10",
         "modified":"2012-04-21 23:44:10"
      },
      {
         "id":"16",
         "parent_id":"2",
         "lft":"29",
         "rght":"34",
         "name":"Category Name",
         "icon":"",
         "created":"2012-04-21 23:44:35",
         "modified":"2012-04-21 23:45:37"
      },
      {
         "id":"17",
         "parent_id":"2",
         "lft":"35",
         "rght":"60",
         "name":"Category Name",
         "icon":"",
         "created":"2012-04-21 23:44:52",
         "modified":"2012-04-21 23:45:53"
      },
      {
         "id":"18",
         "parent_id":"2",
         "lft":"61",
         "rght":"62",
         "name":"Category Name",
         "icon":"",
         "created":"2012-04-21 23:46:05",
         "modified":"2012-04-21 23:46:05"
      },
      {
         "id":"19",
         "parent_id":"2",
         "lft":"63",
         "rght":"70",
         "name":"Category Name",
         "icon":"",
         "created":"2012-04-21 23:46:19",
         "modified":"2012-04-21 23:46:19"
      }
   ],
   "success":1
}

Upvotes: 0

Views: 268

Answers (6)

Jonik
Jonik

Reputation: 151

Try Gson by Google. Here is example. How to parse it to Array List

List<Category> categories = gson.fromJson(json, new TypeToken<Collection<Category>>() {}.getType());

Upvotes: 1

Anup Cowkur
Anup Cowkur

Reputation: 20553

Get the json array and get individual nested objects and arrays inside it one by one. Then use get(String key) method to get the value of individual strings inside the objects.

In your case, the code would be:

JSONObject json_data = new JSONObject(result);
JSONArray json_array = new JSONArray(json_data.getString("Category"));
JSONObject temp;
for(i=0;i<json_array.length();i++)
{
temp = json_array.getJSONObject(i);

String id = temp.get("id").toString();
String parent_id = temp.get("parent_id").toString();
.
.
.
}
String success_value=json_data.get("success");

Upvotes: 1

S&#248;ren Lorentzen
S&#248;ren Lorentzen

Reputation: 866

Have a look at Google's JSON implementaion in Java.

GSON is just awesome. http://code.google.com/p/google-gson/

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

try like this

JSONObject object = new JSONObject(result);
JSONArray Jarray = object.getJSONArray("Category");

for (int i = 0; i < Jarray.length(); i++) {
    JSONObject Jasonobject = Jarray.getJSONObject(i);

Upvotes: 0

Mikkel L&#248;kke
Mikkel L&#248;kke

Reputation: 3749

I would probably use a 3rd party library like Jackson: http://jackson.codehaus.org/

Then you can do something like this:

ObjectMapper mapper = new ObjectMapper();
JsonParser jp = mapper.getJsonFactory().createJsonParser(jsonBody);
JsonNode rootNode = mapper.readTree(jp);

Where jsonBody is your string. You can then use the get() method of JsonNode to extract your data in whichever way you find most useful for your purpose. There are plenty of examples on the Internet to get you started.

Upvotes: 0

Nermeen
Nermeen

Reputation: 15973

JSONObject jasonobject = new JSONObject(yourString);
JSONArray jarray = Jasonobject.getJSONArray("Category");

for (int i = 0; i < Jarray.length(); i++) { ...}

check http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/

Upvotes: 0

Related Questions