1

There are codes I am using:

try {
       resultObject = new JSONObject(URLDecoder.decode(result, "UTF-8"));// no problem here
       resultJsonArray = resultObject.getJSONArray("data"); // error comes when run this line.

    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

This is the whole error information:

org.json.JSONException: Value [{"first_name":"aig","phone_no":"3659428","passcode":"aig","last_name":"aig","user_id":"03343785-2714-43a5-a566-f4d9877ccafa","email_id":"[email protected]"},{"first_name":"aig","phone_no":"635448448","passcode":"aig","last_name":"aig","user_id":"5dc26dcc-3f81-434a-b293-48438f2f920a","email_id":"[email protected]"}] at data of type java.lang.String cannot be converted to JSONArray

After being formatted(http://jsonformatter.curiousconcept.com/), the JSON string will be like this:

{  
   "response":"Success",
   "tablename":"USER_INFO",
   "transaction_type":"MODIFICATION_PULL_RESPONSE",
   "data":"[{\"first_name\":\"aig\",\"phone_no\":\"3659428\",\"passcode\":\"aig\",\"last_name\":\"aig\",\"user_id\":\"03343785-2714-43a5-a566-f4d9877ccafa\",\"email_id\":\"[email protected]\"},{\"first_name\":\"aig\",\"phone_no\":\"635448448\",\"passcode\":\"aig\",\"last_name\":\"aig\",\"user_id\":\"5dc26dcc-3f81-434a-b293-48438f2f920a\",\"email_id\":\"[email protected]\"}]"
}
5
  • There must not be any String or Space before starting of JSONArray or JSONObject i.e.root must be like this [ or '{'. Commented Dec 19, 2014 at 6:34
  • Possible duplicate of Value of type java.lang.String cannot be converted to JSONArray and JSONException: Value of type java.lang.String cannot be converted to JSONObject. Commented Dec 19, 2014 at 6:37
  • @jww I checked all those threads before I asked my question. Somehow my problem is still here after trying those solutions. Commented Dec 19, 2014 at 6:44
  • @Nitin Misra I checked my json string using jsonformatter.curiousconcept.com. It is valid json string. Commented Dec 19, 2014 at 6:44
  • Hey, friends. I tried my codes in Eclipse, and they works fine. But they don't work in Android Studio. Any idea will be appreciated. Thanks Commented Dec 19, 2014 at 6:52

3 Answers 3

0

Please avoid the ""(Double quote characters) directly cause JSON through an exception while parsing it, always use \ Before these characters,

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string. .It may help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Actually I tried my codes in Eclipse, it works. But when I trying to do that in Android Studio, it doesn't. It is wired.
0

you doing wrong. try like below code:

try {

   resultJsonArray = resultObject.getJSONArray(result);

} catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

3 Comments

Hi, MunimJi, thanks. I tried your codes, Same error.
check my edited answer. i think it should be like this.
Hi, MunimJi, thanks. Please check the solution I just posted. It don't know why it works. Cheers.
0
After testing for a while, I solved it in a wired way:

**Before:**

       try {
          resultObject = new JSONObject(URLDecoder.decode(result, "UTF-8"));// no problem here
          resultJsonArray = resultObject.getJSONArray("data"); // error comes when run this line.

       } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
       }


**After**

      try {
          resultObject = new JSONObject(URLDecoder.decode(result, "UTF-8"));// no problem here
          resultJsonArray = new JSONArray(resultObject.get("data").toString()); // Works!!!

       } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
       }

It is really wired. I have no idea why this works.

2 Comments

what the h.. is wired
I got the same issue! Your solution resultJsonArray = new JSONArray(resultObject.get("data").toString()); is working for me. Does someone know what is happening here? thank you anyway for this workaround, btw: i guess you mean "weird" not "wired"

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.