简体   繁体   中英

JSONException: Value ___ of type java.lang.String cannot be converted to JSONObject

I am learning retrieving data from database or website in json format. I have written the code for parsing the data. The problem is I am getting JsonException when I put string in jsonObject constructor. I have been searching to solve this problem but seems like my code doesn't have any problems. The json data I am getting from internet also seems ok to me. I want to seek experts help here for reading this json format data. Here is my code:

JSONObject json=null;

    Toast.makeText(DisplaySubjectsInList.this, "json method", Toast.LENGTH_SHORT).show();

    String temp ="";
    temp = sb.toString();


    try 
    {
        JSONObject jsonObj = new JSONObject(temp);

        JSONArray jsonArray = jsonObj.getJSONArray("contacts");


        ListOfSubjects = new String[jsonArray.length()];

        //subjectAddresses = new String[jsonArray.length()];


        for(int i=0; i<2; i++)
        {
            json = jsonArray.getJSONObject(i);

            ListOfSubjects[i] = json.getString("name"); 
            //subjectAddresses[i]=json.getString("Adress");

            Toast.makeText(DisplaySubjectsInList.this, ListOfSubjects[i], Toast.LENGTH_SHORT).show();
        }

    } 
    catch (JSONException e) 
    {
        Toast.makeText(DisplaySubjectsInList.this, "json failed", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

Stack Trace:

06-30 16:28:50.553: W/System.err(1629): org.json.JSONException: Value contacts of type java.lang.String cannot be converted to JSONObject
06-30 16:28:50.553: W/System.err(1629):     at org.json.JSON.typeMismatch(JSON.java:107)
06-30 16:28:50.553: W/System.err(1629):     at org.json.JSONObject.<init>(JSONObject.java:158)
06-30 16:28:50.553: W/System.err(1629):     at org.json.JSONObject.<init>(JSONObject.java:171)
06-30 16:28:50.553: W/System.err(1629):     at com.example.readingdatabaseinternet.DisplaySubjectsInList.convertFromJsonFormat(DisplaySubjectsInList.java:106)
06-30 16:28:50.553: W/System.err(1629):     at com.example.readingdatabaseinternet.DisplaySubjectsInList.onCreate(DisplaySubjectsInList.java:54)
06-30 16:28:50.553: W/System.err(1629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-30 16:28:50.553: W/System.err(1629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-30 16:28:50.553: W/System.err(1629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-30 16:28:50.553: W/System.err(1629):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-30 16:28:50.553: W/System.err(1629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-30 16:28:50.553: W/System.err(1629):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 16:28:50.553: W/System.err(1629):     at android.os.Looper.loop(Looper.java:130)
06-30 16:28:50.553: W/System.err(1629):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-30 16:28:50.553: W/System.err(1629):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 16:28:50.553: W/System.err(1629):     at java.lang.reflect.Method.invoke(Method.java:507)
06-30 16:28:50.553: W/System.err(1629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-30 16:28:50.553: W/System.err(1629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-30 16:28:50.553: W/System.err(1629):     at dalvik.system.NativeStart.main(Native Method)

Here is the link to json format data: Json

I have also tried non formated json but it is returning <p>Forbidden</p>

Regards

You need to encode your json without formatting it. The parser doesn't like 'beautified' json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM