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":"aig.science@gmail.com"},{"first_name":"aig","phone_no":"635448448","passcode":"aig","last_name":"aig","user_id":"5dc26dcc-3f81-434a-b293-48438f2f920a","email_id":"aig.science@gmail.com"}] 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\":\"aig.science@gmail.com\"},{\"first_name\":\"aig\",\"phone_no\":\"635448448\",\"passcode\":\"aig\",\"last_name\":\"aig\",\"user_id\":\"5dc26dcc-3f81-434a-b293-48438f2f920a\",\"email_id\":\"aig.science@gmail.com\"}]"
}
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
Sam003
  • 540
  • 6
  • 17
  • There must not be any `String` or `Space` before starting of `JSONArray` or `JSONObject` i.e.root must be like this `[` or '{'. – Nitin Misra Dec 19 '14 at 06:34
  • Possible duplicate of [Value of type java.lang.String cannot be converted to JSONArray](http://stackoverflow.com/q/18174064/608639) and [JSONException: Value of type java.lang.String cannot be converted to JSONObject](http://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject). – jww Dec 19 '14 at 06:37
  • @jww I checked all those threads before I asked my question. Somehow my problem is still here after trying those solutions. – Sam003 Dec 19 '14 at 06:44
  • @Nitin Misra I checked my json string using http://jsonformatter.curiousconcept.com/. It is valid json string. – Sam003 Dec 19 '14 at 06: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 – Sam003 Dec 19 '14 at 06:52

3 Answers3

0

you doing wrong. try like below code:

try {

   resultJsonArray = resultObject.getJSONArray(result);

} catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
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.

Charan Ghate
  • 1,384
  • 15
  • 32
  • 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. – Sam003 Dec 19 '14 at 06:51
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.

Sam003
  • 540
  • 6
  • 17
  • what the h.. is wired – vilpe89 Dec 19 '14 at 13:31
  • 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" – Tino Oct 11 '15 at 15:06