5

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"
                ]
            ]
1
  • What have you tried so far? Can you show your code? Commented Dec 28, 2017 at 6:59

5 Answers 5

4

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

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

Comments

1

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"
            ]
        ]
]

1 Comment

maybe you can add "{" or "[" at beginning and end. And then parse it, it will work.
1

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

Comments

1

Here is the structure.

Object

  rows->Array

     -Array
     -Array
     -Array

Comments

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

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.