Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get sub-array from JSON

Tags:

java

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.

like image 996
user2579475 Avatar asked Feb 13 '26 23:02

user2579475


1 Answers

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");
} 
like image 69
Ravi K Thapliyal Avatar answered Feb 15 '26 19:02

Ravi K Thapliyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!