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) {
        }
    })
6
  • 2
    replace slashes with "" maybe help you Commented Oct 3, 2016 at 6:16
  • i tried jsonStr = jsonStr.replaceAll("[]",""); but no user @dariush f Commented Oct 3, 2016 at 6:27
  • you replaced brackets ([]) , do this : jsonStr = jsonStr.replaceAll("\",""); Commented Oct 3, 2016 at 6:28
  • after replaceing am getting error "Value [{ of type java.lang.String cannot be converted to JSONArray" @dariush f Commented Oct 3, 2016 at 6:51
  • @BinilSurendran Any luck? Commented Oct 3, 2016 at 12:01

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

6 Comments

json array key means , there is no key for jsonArray, In the result i have only one array@ascii_walker
for jsonFile like this: { "orgList": [ { "orgInfo": { "id": 1, "orgName": "Organization X" }, {},{}...} Key for jsonArray is : orgList
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
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
after replaceing am getting error "Value [{ of type java.lang.String cannot be converted to JSONArray" @ascii_walker
|
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...

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.