1

我正在学习以 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

我也尝试过非格式化的json,但它正在返回<p>Forbidden</p>

问候

4

1 回答 1

0

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

于 2013-06-30T11:46:22.373 回答