John
John

Reputation: 281

Extracting an array from a JSON object

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
                        }
                    }
                }
            } 

Upvotes: 1

Views: 294

Answers (4)

Make it Simple
Make it Simple

Reputation: 1882

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);

Upvotes: 0

Kishor Raskar
Kishor Raskar

Reputation: 116

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

Upvotes: 0

Rahul
Rahul

Reputation: 45040

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.
            }
        }
}

Upvotes: 0

Menno
Menno

Reputation: 12621

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.

Upvotes: 1

Related Questions