user2579475
user2579475

Reputation: 1101

Get sub-array from JSON

I parsing some data from a json file. Here is my JSON File.

[
     {

       "topic": "Example1", 
       "contact": [
            {
                "ref": [
                    1
                ], 
                "corresponding": true, 
                "name": "XYZ"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ZXY"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ABC"
            }, 
            {
                "ref": [
                    1, 
                    2
                ], 
                "name":"BCA"
            }
        ] , 

        "type": "Presentation"
     }, 
    {

       "topic": "Example2", 
       "contact": [
            {
                "ref": [
                    1
                ], 
                "corresponding": true, 
                "name": "XYZ"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ZXY"
            }, 
            {
                "ref": [
                    1
                ], 
                "name": "ABC"
            }, 
            {
                "ref": [
                    1, 
                    2
                ], 
                "name":"BCA"
            }
        ] , 

        "type": "Poster"
     }
]

I can fetch and store data one by one. Like this one

JSONArray getContactsArray = new JSONArray(jsonObject.getString("contact"));
for(int a =0 ; a < getContactsArray.length(); a++)
{
    JSONObject getJSonObj = (JSONObject)getContactsArray.get(a);
    String Name = getJSonObj.getString("name");
} 

1)Now, my question is there any way to get all name values for each array with single query. 2) Can I get all those values in an Array ?

Please correct me, if I am doing anything wrong. Thank you.

Upvotes: 3

Views: 14826

Answers (3)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Iteration cannot be avoided here as org.json and other Json parsers as well provide random access to objects but not to their properties collectively (as a collection). So, you can't query something like "all name properties of all contact objects" unless you probably get a Json parser like Gson to unmarshall it that way.

But, that's too much to just avoid a for loop when you can definitely shorten the parse by making use of the appropriate API methods to avoid unnecessary object casts.

JSONArray contacts = jsonObject.getJSONArray("contact");
String[] contactNames = new String[contacts.length()];
for(int i = 0 ; i < contactNames.length; i++) {
    contactNames[i] = contacts.getJSONObject(i).getString("name");
} 

Upvotes: 5

Tanu Garg
Tanu Garg

Reputation: 3067

Try this:

Create JSONObject of your file and try to get array of all names and iterate it to get all values.

public static String[] getNames(JSONObject jo) {

        int length = jo.length();
        if (length == 0) {
            return null;
        }
        Iterator i = jo.keys();
        String[] names = new String[length];
        int j = 0;
        while (i.hasNext()) {
            names[j] = (String) i.next();
            j += 1;
        }       
        return names;
    }

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Better to use a json parser such as GSon or Jackson to marshall your json to a java object. Then you can write utitlity method in your java class to retrieve all the names in that object.

Upvotes: 0

Related Questions