1

I have a JSON Object of the following and I need to parse the path strings inside the web array into an new JSON array.

"taxonomy": {
    "source": {
        "master": {
            "_id": "5000",
            "path": "/Appliances/Refrigerators/French Door Bottom Freezers"
        },
        "web": [
            {
                "_id": "6686",
                "path": "/Appliances/Refrigerators/French Door Bottom Freezers"
            },
            {
                "_id": "7686",
                "path": "/Appliances/Refrigerators/Bottom Freezers"
            }
        ],

    },

},

I have written till this but I'm not sure how to get all the path inside the web array.

                JSONObject jsonTaxonomy= _blob.optJSONObject("taxonomy");
            if(jsonTaxonomy!=null)
            {
                if(!jsonTaxonomy.isNull("source"))
                {
                    JSONObject jsonTaxonomySource= jsonTaxonomy.optJSONObject("source");
                    if(!jsonTaxonomySource.isNull("web"))
                    {
                        JSONArray jsonTaxonomySourceWeb= jsonTaxonomySource.optJSONArray("web");
                        if(jsonTaxonomySourceWeb!=null && jsonTaxonomySourceWeb.length()>0)
                        {
                            //Got inside the array
                        }
                    }
                }
            } 
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
John
  • 281
  • 3
  • 14

4 Answers4

1

Without providing you with a full answer, I'm convinced you'll be able to find your answer by debugging this method and stopping it at the most inner if(). You'll be able to of what jsonTaxonomySearsWeb consists and thus how to get its values.

Menno
  • 12,175
  • 14
  • 56
  • 88
  • Will using jsonTaxonomySearsWeb.getJSONObject(i).getString("path") and adding them to an array will work??? – John Apr 29 '13 at 10:23
  • I'm no expert on `JSONObject`, but if this is part of a loop and `i` is provided by it, this probably will work. – Menno Apr 29 '13 at 10:26
0

Modify your code to something like this:-

JSONObject jsonTaxonomy= _blob.optJSONObject("taxonomy");
if(jsonTaxonomy!=null)
{
        JSONObject jsonTaxonomySource = jsonTaxonomy.optJSONObject("source");
        if(jsonTaxonomySource!=null)
        {
            JSONArray jsonTaxonomySearsWeb= jsonTaxonomySource.optJSONArray("web");
            if(jsonTaxonomySearsWeb!=null)
            {
                // Traverse through your JSONArray and get each Object & extract path from it.
            }
        }
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

I am bit unclear about the question. But As per my understanding you want to parse the JSON if you want to do that in java then you can use GSON jar from google...you can also check simple example here Gson handle object or array

Community
  • 1
  • 1
0

Try like this...

      groups = json.getJSONArray(TAG_GROUP);
      System.out.println("Result Success+++"+groups);
      for (int i = 0; i < groups.length(); i++) {
      JSONObject c = groups.getJSONObject(i);
      String source = c.getString(TAG_SOURCE);
      System.out.println("Checking ::"+source);
      String lname = c.getString(TAG_PATH);
      HashMap<String, String> map = new HashMap<String, String>();
      map.put(TAG_SOURCE, source);
      map.put(TAG_PATH,path);
      weblist.add(map);       //weblist is your arraylist for both values
      webpathlist.add(path);   //webpathlist is your arraylist for path
       }
      List<String> newList = new ArrayList<String>();
      newList.addAll(weblist);
Make it Simple
  • 1,832
  • 5
  • 32
  • 57