Aj 27
Aj 27

Reputation: 2427

Parse JSON Array without Key

I am trying to parse the jsonArray but unable to understand this format,How to parse this type of jsonArray? Can anyone help me?

  "rows": [
                [
                    "/farmfresh",
                    "20171211",
                    "4"
                ],
                [
                    "/farmfresh/product/d",
                    "20171215",
                    "4"
                ],
                [
                    "/farmfresh/product/h",
                    "20171222",
                    "2"
                ]
            ]

Upvotes: 5

Views: 11643

Answers (5)

Sourabh Bans
Sourabh Bans

Reputation: 3134

Here is the structure.

Object

  rows->Array

     -Array
     -Array
     -Array

Upvotes: 1

Ratilal Chopda
Ratilal Chopda

Reputation: 4220

Try this

try 
{    
    JSONObject resObject = new JSONObject("your json response");    
    JSONArray jsonArray = resObject.getJSONArray("rows");        

    for (int i = 0; i < jsonArray.length(); i++) {        
       JSONArray jsonArray1 = jsonArray.getJSONArray(i);

       for (int j = 0; j < jsonArray1.length(); j++) {

          Log.i("Value","->" +jsonArray1.getString(j));
       }
   }

} 
catch (JSONException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

OUTPUT

enter image description here

Upvotes: 4

Shalauddin Ahamad Shuza
Shalauddin Ahamad Shuza

Reputation: 3657

First of all your JSON data is not valid. The valid data will be

 { "rows": [
            [
                "/farmfresh",
                "20171211",
                "4"
            ],
            [
                "/farmfresh/product/d",
                "20171215",
                "4"
            ],
            [
                "/farmfresh/product/h",
                "20171222",
                "2"
            ]
        ]
}

Now we can parse this valid json as give below. This is for android using default JSON parse. Here you

void parseJsonString(String jsonString) {
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONArray jsonArray = jsonObject.getJSONArray("rows");
        for (int i = 0; i < jsonArray.length(); i++) {
            Log.d("jsonParse", "row position    =   " + String.valueOf(i));
            JSONArray jsonRow = jsonArray.getJSONArray(i);
            for (int j = 0; j < jsonRow.length(); j++) {
                String value = jsonRow.get(j).toString();
                Log.d("jsonParse", "value at " + String.valueOf(j) + " position is  " + value);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

This code is done for android. Here the JSON String is passed as the argument of the parseJsonString method. It will print the values in logcat as below

enter image description here

Upvotes: 1

Nikhil Borad
Nikhil Borad

Reputation: 2085

String data = "{"+"YOUR_DATA"+"}";
 try {
        JSONObject object = new JSONObject(data);
        JSONArray itemArray = object.getJSONArray("rows");
        for (int i = 0; i < itemArray.length(); i++) {
            JSONArray innerItemArray=  itemArray.getJSONArray(i);
            for (int j = 0; j < innerItemArray.length(); j++) {
                String value=innerItemArray.getString(j);
                Log.e("json", "="+value);
            }

        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Hope it helps..

Upvotes: 0

Geethakrishna Juluri
Geethakrishna Juluri

Reputation: 540

To my knowledge, we cannot parse the JSON you shared. It always has to start with "{" or "[". If it is an array it should look like below JSON

["rows": [
            [
                "/farmfresh",
                "20171211",
                "4"
            ],
            [
                "/farmfresh/product/d",
                "20171215",
                "4"
            ],
            [
                "/farmfresh/product/h",
                "20171222",
                "2"
            ]
        ]
]

Upvotes: 1

Related Questions