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
                        }
                    }
                }
            } 
2
  • if(jsonTaxonomy!=null){ if(!jsonTaxonomy.isNull("source")){ }} can be replaced with if(jsonTaxonomy!=null && !jsonTaxonomy.isNull("source")){ } Commented Apr 29, 2013 at 10:12
  • u need all source value to be added in web array.. Commented Apr 29, 2013 at 10:22

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Will using jsonTaxonomySearsWeb.getJSONObject(i).getString("path") and adding them to an array will work???
I'm no expert on JSONObject, but if this is part of a loop and i is provided by it, this probably will work.
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.
            }
        }
}

Comments

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

Comments

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

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.