0

How to parse JSONArray inside JSONObject?. Here is the JSON response I'm getting from the server.

{
"searchdata": {
    "titles": [
        "<b>Laptop</b> - Wikipedia, the free encyclopedia",
        "<b>laptop</b> - definition of <b>laptop</b> by the Free Online Dictionary ..."
    ],
    "desc": [
        "A <b>laptop</b> computer is a personal computer for mobile use. A <b>laptop</b> has most of the same components as a desktop computer, including a display, a keyboard, a ...",
        "lap·top (l p t p) n. A portable computer small enough to use on one&apos;s lap. <b>laptop</b> [ˈlæpˌtɒp], <b>laptop</b> computer. n (Electronics &amp; Computer Science / Computer ..."
    ],
    "links": [
        "http://en.wikipedia.org/wiki/Laptop",
        "http://www.thefreedictionary.com/laptop"
    ],
    "nextpage": ""
}
}

I'm able to get JSONObject but how to get JSONArray one by one, so that I can fix them in the listview.

I want to show the values of each array in a single row of the listview and so on....

Any help will be appreciated.

Perception
  • 79,279
  • 19
  • 185
  • 195
Anupam
  • 3,742
  • 18
  • 55
  • 87
  • You can use GSON library for JSON parsing (instead of doing it manually). Look at example here: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html – Piotr Feb 28 '13 at 08:04

3 Answers3

1

Very easy..

you need to fix code like this:

//jsonString is your whole JSONString which you have shown above

JSONObject jObj = new JSONObject(jsonString);
JSONObject jSearchData = jObj.getJSONObject("searchdata");
JSONArray jTitles = jSearchData.getJSONArray("titles");
JSONArray jDesc= jSearchData.getJSONArray("desc");
JSONArray jLinks= jSearchData.getJSONArray("links");
String nextPage = jSearchData.getString("nextpage");
//and so on and so forth

For fetching the array items and show it into a listview:

//you can iterate over each item and add it to an ArrayList like this:

//i am showing you a single one,follow the same process for other arrays:

ArrayList titlesArray = new ArrayList();

for (int i = 0; i < jTitles.length(); i++) {
String item = jTitles.getString(i);
titlesArray.add(item);

}

Next you make this arraylist a source to a listview like this:

 // Get a handle to the list view
    ListView lv = (ListView) findViewById(R.id.ListView01);
 lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
            android.R.layout.simple_list_item_1, titlesArray));
Nezam
  • 4,122
  • 3
  • 32
  • 49
  • Hey Nezam, I have added each item in the array, so now how can I call those items in the listview. Can you help me with that? – Anupam Feb 28 '13 at 09:06
  • just set it to the adapter. Like.. in place of titlesArray supply your array in the last line of the code which i posted – Nezam Feb 28 '13 at 09:07
  • http://stackoverflow.com/questions/15131306/fix-jsonresponse-on-listview/15131399#15131399 – Anupam Feb 28 '13 at 09:07
  • Can you please check the question I have posted. – Anupam Feb 28 '13 at 09:11
  • If you would have followed my answer here.. you would not have needed to post a new question.Anyways,i answered there.Check it out. – Nezam Feb 28 '13 at 09:16
1

Consider that your top level JSON will be parsed into a JSONObject, and that subsequently you can request to receive from it, any sub level objects and/or arrays via the methods getJSONObject(name) and getJSONArray(name). Your arrays of interest are two levels deep in the JSON hierarchy, so you will need something like this:

String json = ...;
JSONObject rootObj = new JSONObject(json);
JSONObject searchObj = rootObj.getJSONObject("searchdata");
JSONArray titlesObj = searchObj.getJSONArray("titles");
JSONArray descsObj = searchObj.getJSONArray("desc");
JSONArray linksObj = searchObj.getJSONArray("links");

You can iterate any of the arrays as such (using titles as an example):

for(int i = 0; i < titlesObj.length(); i++) {
    String title = titlesObj.getString(i);
}
Perception
  • 79,279
  • 19
  • 185
  • 195
  • While iterating I'm getting error stating - `Type Mismatch. Cannot convert from Object to String`at `String title = titlesObj.get(i);` Why is that? – Anupam Feb 28 '13 at 07:34
  • @Anupam - method should have been `getString`, not `get`. I've fixed the code sample. Also, don't forget to accept whichever answer helped you the most! – Perception Feb 28 '13 at 07:36
  • @Nezam Yes because I saw this answer first and it solved my problem. Anyways thankyou for your answer. – Anupam Feb 28 '13 at 09:01
  • Perception was just 22 secs fast with a mistake in his answer.Anyways.glad you got it solved – Nezam Feb 28 '13 at 09:03
  • @Nezam, brow beating the OP this way is highly inappropriate. You made a [large edit](http://stackoverflow.com/posts/15129794/revisions) to your answer some time after it was posted which (significantly) altered it. – Perception Feb 28 '13 at 11:53
  • if you would look closely to that same large edit.You would find that even prior to that the actual answer (which even you answered) was already posted.I simply posted the list view adapter code in the large edit – Nezam Feb 28 '13 at 12:00
0

it will be help:

JSONArray  titles = new jSONObject(jsonstring).getJSONObject("searchdata").getJSONArray("titles");

JSONArray  desc  = new jSONObject(jsonstring).getJSONObject("searchdata").getJSONArray("desc");

JSONArray  links = new jSONObject(jsonstring).getJSONObject("searchdata").getJSONArray("links");
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23