gpt4 book ai didi

java - 将 JSONArray 转换为 JSONObject 时出现 ClassCastException

转载 作者:行者123 更新时间:2023-11-30 09:17:11 24 4
gpt4 key购买 nike

这是我的代码 fragment :

    jArray = jChild.getJSONArray("users");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = (JSONObject) jArray.get(i); //Exception thrown in this line

}

编辑:

日志:

09-30 09:06:23.404: E/AndroidRuntime(4011): java.lang.RuntimeException: An error occured while executing doInBackground()
09-30 09:06:23.404: E/AndroidRuntime(4011): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-30 09:06:23.404: E/AndroidRuntime(4011): Caused by: java.lang.ClassCastException: java.lang.String
09-30 09:06:23.404: E/AndroidRuntime(4011): at com.example.DbAccess.loadMonthView(DbAccess.java:196)

这是我的 jsonArray:

 [{"uid":"6","status":"absent","name":"xyz"},{"uid":"7","status":"absent","name":"abc Paul"}]

我想从 JSONArray 中检索 JSONObject

最佳答案

[ // represents json array node
{ // represents json obeject node
"uid": "6",
"status": "absent",
"name": "xyz"
},
{
"uid": "7",
"status": "absent",
"name": "abc Paul"
}
]

您拥有的是一个 JSONArray。您需要解析值并获取字符串。

     JSONArray jr = new JSONArray("myjsonstring");
for(int i=0;i<jr.length();j++)
{
JSONOBject jb = jr.getJSONObject(i);
String uid = jb.getString("uid");
String status = jb.getString("status");
String name = jb.getString("name");
}

关于java - 将 JSONArray 转换为 JSONObject 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19086180/

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