1

I am getting json Exception while trying to parse a string to jsonArray. I am using loopj method for json Parsing. This is the error :

java.lang.String cannot be converted to JSONArray

And this is the json I am trying to parse :

[{\"displayName\":\"Thiruvananthapuram\",\"desc\":\"Partly cloudy\",\"cloudCover\":\"5\",\"dateTime\":\"Sunday October 02\",\"humidity\":\"85\",\"visibility\":\"10\",\"tempCelcius\":\"29\",\"iconClass\":\"PartlyCloudy-s\"}]

My code is

    AsyncHttpClient client = new AsyncHttpClient();
   client.get("url", new AsyncHttpResponseHandler() {
    @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

        try {
        String jsonStr = new String(responseBody, "UTF-8");
        Log.e("Tag ", "on Result " + jsonStr);
        JSONArray jsonarray = new JSONArray(jsonStr);
                Log.e("Tag ", "jsonArr length " + jsonarray.length());
        }
        catch (UnsupportedEncodingException e)
        {
          e.printStackTrace();
        }
        catch (Exception e)
        {
          e.printStackTrace();
            }
        }
        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        }
    })
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Binil Surendran
  • 2,524
  • 6
  • 35
  • 58

2 Answers2

1

First you have to get jsonObject using jsonString using line below

JSONObject jsnobject = new JSONObject(jsonStr);

Then you can iterate through the jsonObject to get jsonArray object.

JSONArray jsonArray = jsnobject.getJSONArray("your json array key");
iamabhaykmr
  • 1,803
  • 3
  • 24
  • 49
  • json array key means , there is no key for jsonArray, In the result i have only one array@ascii_walker – Binil Surendran Oct 03 '16 at 06:25
  • for jsonFile like this: { "orgList": [ { "orgInfo": { "id": 1, "orgName": "Organization X" }, {},{}...} Key for jsonArray is : orgList – iamabhaykmr Oct 03 '16 at 06:27
  • here the reult is JsonArray not JsonObject [{\"displayName\":\"Thiruvananthapuram\",\"desc\":\"Partly cloudy\",\"cloudCover\":\"5\",\"dateTime\":\"Sunday October 02\",\"humidity\":\"85\",\"visibility\":\"10\",\"tempCelcius\":\"29\",\"iconClass\":\"PartlyCloudy-s\"}] @ascii_walker – Binil Surendran Oct 03 '16 at 06:32
  • You can replace "\" with "" to make it look like normal json then you can parse it by iterating through key value pair of json array – iamabhaykmr Oct 03 '16 at 06:37
  • after replaceing am getting error "Value [{ of type java.lang.String cannot be converted to JSONArray" @ascii_walker – Binil Surendran Oct 03 '16 at 06:51
  • Its seem you dont have key in you json string append a key in your json string: "jsonArrayKey":[{a:b},{aa:bb},{},....] – iamabhaykmr Oct 03 '16 at 07:01
0

you have to replace the "\" to "" then convert into the JsonArray:-

example:-

 String jsonStr = new String(responseBody, "UTF-8");

jsonStr = jsonStr.replace("\","");

// now convert it into JsonArray
  JSONArray jsonarray = new JSONArray(jsonStr);

hope this help you...

Sarbjyot
  • 136
  • 2
  • 8