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.

1
  • You can use GSON library for JSON parsing (instead of doing it manually). Look at example here: javacodegeeks.com/2011/01/… Commented Feb 28, 2013 at 8:04

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

5 Comments

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?
just set it to the adapter. Like.. in place of titlesArray supply your array in the last line of the code which i posted
Can you please check the question I have posted.
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.
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);
}

6 Comments

While iterating I'm getting error stating - Type Mismatch. Cannot convert from Object to Stringat String title = titlesObj.get(i); Why is that?
@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!
@Nezam Yes because I saw this answer first and it solved my problem. Anyways thankyou for your answer.
Perception was just 22 secs fast with a mistake in his answer.Anyways.glad you got it solved
@Nezam, brow beating the OP this way is highly inappropriate. You made a large edit to your answer some time after it was posted which (significantly) altered it.
|
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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.