JSONException:java.lang.String类型的值___无法转换为JSONObject

时间:2013-06-30 11:40:31

标签: java android json

我正在学习以json格式从数据库或网站检索数据。我编写了解析数据的代码。问题是当我把字符串放在jsonObject构造函数中时,我得到了JsonException。我一直在寻找解决这个问题,但似乎我的代码没有任何问题。我从互联网上获得的json数据对我来说也不错。我想在这里寻求专家帮助来阅读这个json格式的数据。这是我的代码:

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();
    }

堆栈追踪:

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)

以下是json格式数据的链接:Json

我还尝试过非formated json,但它正在返回<p>Forbidden</p>

此致

1 个答案:

答案 0 :(得分:0)

您需要对json进行编码而不进行格式化。解析器不喜欢'美化'json。