4

I have used this site for years, but never have posted. I am stumped and hope someone might be able to help me.

I am using very similar code to what Slicekick posted here JSON parsing app "no data"?, but can not seem to figure out how to parse this JSON file. I have edited his JSON info to reflect the exact format I am trying to parse to save space/time. How do I parse the "Results" so I can query "Name" and "Type"? Like his problem with "Similar" and "Info", how do I parse "Info" and "Results" in the file below?

Here is an example of a JSON file edited in the exact format I am using:

{
"head": {
    "title": "Music",
    "status": "200"
},
"Info": [
    {
        "Name": "Mos Def",
        "Type": "music",
        "Results": [
            {
                "Name": "Talib Kweli",
                "Type": "music"
            },
            {
                "Name": "Black Star",
                "Type": "music"
            },
            {
                "Name": "Little Brother",
                "Type": "music"
            }
         ]
    }, 
    {
        "Name": "Mos Def",
        "Type": "Vehicles",
        "Results": [
            {
                "Name": "Chevy",
                "Type": "Car"
            },
            {
                "Name": "Ford",
                "Type": "Car"
            },
            {
                "Name": "Pontiac",
                "Type": "Car"
            }
         ]
      }
   ]
}

The part of my code that may be of interest is:

... I do a httpget ... that gets built into a StringBuilder ... creates a JSONObject with the results of StringBuilder

   jArray = new JSONObject(result);

... then returns that

Then onto...

   JSONArray  Info = json.optJSONArray("Info");

   System.out.println("HERE IS INFO: ");
   System.out.println(Info);
   //System.out.println("HERE IS RESULTS: ");
   //System.out.println(Results);

And basically here is where I am stumped. I put in print messages to try to narrow the issue down.

Parsing "Info" allows me to search: "Name": "Mos Def" "Type": "music" -and- "Name": "Mos Def" "Type": "Vehicles"

Replacing the search of "Info" with "Results" gives me no data. (Not found)

Any ideas?

Community
  • 1
  • 1
Monty
  • 461
  • 5
  • 9

3 Answers3

4

That is for sure because Results is an JSONArray inside the JSONArray Info. So you should try something like this,

JSONArray  Info = json.optJSONArray("Info");
JSONArray Results = null;
for (int i = 0; i < array.length(); i++) {
            JSONObject object = Info.getJSONObject(i);
            Results results = object.getJSONArray("Results");
        }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Thank you very much for this reply. That is all I really needed was a little hint. By using what you posted & what Jitendra posted - I have now have it figured out. Thanks again! I'll post what eventually got it working if you think it may help anyone else. – Monty Jan 06 '12 at 23:08
  • @lalit Bro I am also facing json parsing problem. I have getting JsonException when I create JsonObject, here is the link to my question. (QUESTION)[http://stackoverflow.com/questions/17523805/json-exception-type-mismatch/17523993?noredirect=1#17523993] Kindly help me – Naruto Jul 08 '13 at 14:14
1

You need to call your parsing code as below:

jArray = new JSONObject(result);
for(int i=0;i<jArray.length();i++)
{
    JSONArray  Info = json.optJSONArray("Info");
    for(int j=0;j<Info.length();j++){
    {
         System.out.println("HERE IS INFO: ");
         System.out.println(Info);
         //System.out.println("HERE IS RESULTS: ");
         JSONObject obj=Info.getJSONObject(j);
         JSONArray results=obj.getJSONArray("Results");
         for(int k=0; k<results.length();
         {
             //Process Results
         }
}
jeet
  • 29,001
  • 6
  • 52
  • 53
  • Will this give the output? I don't think so because Results is inside a JSONObject and you are trying to access it directly. – Lalit Poptani Jan 06 '12 at 06:36
  • Yes, it may need some Edition, and I have edited posted accordingly, but still it is not compiled code. – jeet Jan 06 '12 at 06:55
  • By reading your response and Lalit's response I was able to put something together that made it work. I thank you so much for taking the time to post! I will post the working solution I ended up with if you think it may help anyone. – Monty Jan 06 '12 at 23:11
0

Better use GSON, its from Google and parsing JSON using it is piece of cake..!! Here is a good tutorial to kick it off, you will be more happy using GSON.

Link for GSON tutorial

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • Thanks for the reply. I've read up on it and still may check it out soon. Had problems trying to import the jar file for some reason so I moved on (kept trying to figure out the included JSON libraries.) – Monty Jan 06 '12 at 23:05
  • download gson from http://code.google.com/p/google-gson/downloads/list , and once downloaded make a "lib" folder in your droid project and then move it there, after that right click project properties in eclipse and add the library, that's it! – Adil Bhatty Jan 08 '12 at 08:13