简体   繁体   中英

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM