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

jason
  • 6,962
  • 36
  • 117
  • 198

3 Answers3

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"
    }
]
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
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.'}]";
Navin Gupta
  • 793
  • 6
  • 20
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"));
Harish Sharma
  • 360
  • 1
  • 2
  • 7