0

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?

1
  • how can u come to conclusion that 'JSONArray' Gives Multi dimension Array as array[][] ? Commented Sep 12, 2014 at 17:02

2 Answers 2

3

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 JSONObject and then retrieve data from there something like

JSONArray json_array = new JSONArray(jArray);

    for (int i = 0; i < json_array.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();
                    }
                 }
Sign up to request clarification or add additional context in comments.

Comments

0

No, it should not behave in the same way as a multidimensional array.

This is because json_array is not an array, it is a different Java object. Only arrays can be accessed with this specific syntax you've shown above. You will need to look into the documentation for this class to see how to access the information you want. It should provide clear and well-documented methods for getting what you need.

You wouldn't want this syntax to allow you to access the array which backs your class anyways. This would be a breach of encapsulation and be poor design in general.

A good starting point is here http://www.json.org/javadoc/org/json/JSONArray.html

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.