-1

I have a very big constant JSONArray coming from Webservice, in order to reduce data transfer, I wanted to put it in Android. I logged all JSONArray(coming from webservice), put it inside a string and converted to JSONArray. Here is the code I wrote :

String additionString =  ""Additions":[{"AdditionID":0,"Type":-1,"Long":" ","Short":" "},{"AdditionID":8,"Type":6,"Long":"English,"Short":"Eng"},{"AdditionID":11,"Type":2,"Long":"French","Short":"Fr."},{"AdditionID":12,"Type":2,"Long":"German","Short":"Ger."}]";

JSONArray AdditionArray = new JSONArray(additionString);

And I'm getting this error for that :

Value Additions of type java.lang.String cannot be converted to JSONArray

By the way, the string looks like this in Android Studio :

 String additionString =  "\"Additions\":[{\"AdditionID\":0,\"Type\":-1,\"Long\":\" \",\"Short\":\" \"},{\"AdditionID\":8,\"Type\":6,\"Long\":"English,\"Short\":\"Eng\"},{\"AdditionID\":11,\"Type\":2,\"Long\":\"French\",\"Short\":\"Fr.\"},{"AdditionID":12,"Type":2,"Long":"German","Short":"Ger."}]";

How can I convert that String to JSONArray? Thanks.

4
  • the syntax you are using is used to extract json element not put the new one.. Commented Nov 19, 2015 at 9:40
  • @Mohit Why can't I generate a new JSONArray like this? or how can I generate one with that log? Commented Nov 19, 2015 at 9:41
  • 1
    "Additions":[....] is not a json array, more over, it is not valid json at all Commented Nov 19, 2015 at 9:41
  • this answer will help you understand how to make JSONArray Commented Nov 19, 2015 at 9:52

3 Answers 3

2
 ""Additions":[{"AdditionID":0,"Type":-1,"Long":" ","Short":" "},{"AdditionID":8,"Type":6,"Long":"English,"Short":"Eng"},{"AdditionID":11,"Type":2,"Long":"French","Short":"Fr."},{"AdditionID":12,"Type":2,"Long":"German","Short":"Ger."}]"

is neither a valid JSONObject nor a valid JSONArray. A JSONObject starts and ends with {}

{
    "Additions": [
        {
            "AdditionID": 0,
            "Type": -1,
            "Long": " ",
            "Short": " "
        },
        {
            "AdditionID": 8,
            "Type": 6,
            "Long": "English",
            "Short": "Eng"
        }
    ]
}

a valid JSONArray would be

[
    {
        "AdditionID": 0,
        "Type": -1,
        "Long": " ",
        "Short": " "
    },
    {
        "AdditionID": 8,
        "Type": 6,
        "Long": "English",
        "Short": "Eng"
    }
]
Sign up to request clarification or add additional context in comments.

Comments

1

A JSONArraystarts with [] and JSONObject starts with{}.

So if you want to convert String in JSONArray than change your string to

String additionString =  "[{'AdditionID':0,'Type':-1,'Long':' ','Short':' '},
{'AdditionID':8,'Type':6,'Long':'English','Short':'Eng'},
{'AdditionID':11,'Type':2,'Long':'French','Short':'Fr.'},
{'AdditionID':12,'Type':2,'Long':'German','Short':'Ger.'}]";

Comments

1

There is problem in json string you are parsing("Long":"English does not end with ").Also, string should be a jsonobject(within {}) or jsonArray. So,You can do the required one as follows:

String s="{\"Additions\":[{\"AdditionID\":0,\"Type\":-1,\"Long\":\" \",\"Short\":\" \"},{\"AdditionID\":8,\"Type\":6,\"Long\":\"English\",\"Short\":\"Eng\"},{\"AdditionID\":11,\"Type\":2,\"Long\":\"French\",\"Short\":\"Fr.\"},{\"AdditionID\":12,\"Type\":2,\"Long\":\"German\",\"Short\":\"Ger.\"}]}";


        JSONObject jsonObject=new JSONObject(s);
        JSONArray jsonArray=jsonObject.getJSONArray("Additions");

        System.out.print(jsonArray.getJSONObject(0).getString("Type"));

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.