gpt4 book ai didi

java - JSONException : Value ___ of type java. lang.String 无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-12-01 04:35:46 26 4
gpt4 key购买 nike

我正在学习从数据库或网站以 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>

问候

最佳答案

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

关于java - JSONException : Value ___ of type java. lang.String 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com