How to retrieve multidimensional array using json in android

I have a multidimensional array that I want to retrieve using JSON in android. I have the following JSON Structure for this :

  [
      [
        {
          "name": "Banking",
          "data": [
            {
              "vendor_name": "SBI",
              "vendor_id": "1"
            },
            {
              "vendor_name": "ICICI",
              "vendor_id": "2"
            },
            {
              "vendor_name": "BOB",
              "vendor_id": "3"
            }
          ],
          "count": 4
        }
     ]
 ]

I want to get "name",vendor_name,vendor_id in my Strings in android. But I am not getting idea how to do this. Please let me know how to retrieve this.

Answers

try {
            jsonObj = new JSONObject(YourString);
        } catch (JSONException e) {
            Log.v("Error in Parser", " " + e);
        }
    try{

     String name=jsonObj.getString("name"); 
            Data = jsonObj.getJSONArray("data");
                for (int i = 0; i < Data.length(); i++) {       
                        JSONObject jsonObj2 = Data.getJSONObject(j);   
                        String vName = jsonObj2.getString("vendor_name");
                        String vId=jsonObj2.getString("vendor_id");     
                }
}catch(Exception e)
{
}

Multidimensional array from json android

I have this json here:

[
        {
            "ID": "2",
            "Item Description": "Data removed for protection",
            "Link": "Data removed for protection",
            "Image": "Data removed for protection",
            "Valid From": "Data removed for protection",
            "Valid To": "Data removed for protection"
        },
        {
            "ID": "3",
            "Item Description": "Data removed for protection",
            "Link": "Data removed for protection",
            "Image": "Data removed for protection",
            "Valid From": "Data removed for protection",
            "Valid To": "Data removed for protection"
        },
{
            "ID": "4",
            "Item Description": "Data removed for protection",
            "Link": "Data removed for protection",
            "Image": "Data removed for protection",
            "Valid From": "Data removed for protection",
            "Valid To": "Data removed for protection"
        }
    ]

So I download this to a JSONObject called jArray. I then turn this into a JSONArray like so:

JSONArray json_array = new JSONArray(jArray);

However, whenever I try to access it like so: json_array[0][0] the IDE throws an error on it and says: Array type expected; found: 'org.json.JSONArray'.

Now, dont get me wrong, but shouldn’t a JSONArray behave in the same way to access the multidimensional aspect?

 

Answers

JSONArray and normal java arrays are not the same.JSONArray is a different java object. @Kon’s answer provides basic idea of JSONArray.

If you want to access the data from your JSONArray, you can loop through it to get each JSONObjectand then retrieve data from there something like

JSONArray jsonArray = new JSONArray(jArray);

    for (int i = 0; i < jsonArray.length() ; i++) {
              try{
                    JSONObject object1 = jsonArray.getJSONObject(i);
                    int id = object1.getInt("ID");
                    String itemDescription = object1.getString("Item Description");     
                    String link = object1.getString("Link");
                    .....//and so on        
                 }
              catch(Exception e){
                    e.printStackTrace();
                    }
                 }

		
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s