gpt4 book ai didi

android - 解析 JSONArray Android 中的 JSONObject 时出错

转载 作者:行者123 更新时间:2023-11-29 17:50:53 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它通过 HTTP GET 从服务器获取数据,将响应(JSON 格式)存储在一个字符串中。我想解析字符串并获取 JSONArray 中的值。

存储在字符串中的接收到的 JSON 是:

{
"code": 1,
"data": {
"moodGraphData": {
"myself": {
"1": {
"id": "999999999",
"value": "4.3",
"name": "Myself",
"owner": "",
"type": 1,
"children": []
}
},
"my_teams": [],
"my_units": [],
"companies": [
{
"id": "4",
"name": "Testing",
"owner": "Ankur Ankur",
"value": "3.4",
"type": "4",
"children": {
"0": {
"id": "10",
"value": 3.8,
"name": "Mun tiimi",
"owner": "",
"type": 5,
"children": [],
"count": 1
},
"4": {
"id": "31",
"value": 3.05,
"name": "gmail.com",
"owner": "",
"type": 5,
"children": [
{
"id": "27",
"value": 2.3,
"name": "Priidu Team2",
"owner": " ",
"type": 5,
"children": [],
"count": 1
}
],
"count": 2
},
"6": {
"id": "50",
"value": 2.95,
"name": "gmail.com",
"owner": "",
"type": 5,
"children": [
{
"id": "51",
"value": 2.6,
"name": "Testing",
"owner": "Ujjwal Mairh",
"type": 5,
"children": [],
"count": 1
}
],
"count": 2
}
}
}
]
},
"companyTopStatements": [
{
"id": "1",
"text": "I am happy at work today",
"key": "Mood",
"answer": "3.5"
},
{
"id": "8",
"text": "I am using my strengths at work",
"key": "Strengths",
"answer": "3.5"
},
{
"id": "11",
"text": "My opinions matter and I can influence in my work",
"key": "Influence",
"answer": "3.5"
}
],
"companyBottomStatements": [
{
"id": "13",
"text": "I am surrounded by teammates who are motivated and doing great things",
"key": "Team",
"answer": "3.2"
},
{
"id": "14",
"text": "I have a close friends at work",
"key": "Friendship",
"answer": "3.2"
},
{
"id": "15",
"text": "I receive frequent feedback in order to progress in my role",
"key": "Feedback",
"answer": "3.2"
}
]
},
"errors": null
}

我想解析 JSON 数组 "companyTopStatements",并检索 "key" 中的值,它位于 JSONObject 中。

我使用的代码如下:

        JSONObject jsonObj = new JSONObject(Get_MyCompany_JSON);
JSONArray jsonArray = jsonObj.getJSONArray("companyTopStatements");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject curr = jsonArray.getJSONObject(i);
String keyValue = curr.getString("key");
Log.d("Key",keyValue);
}

我无法解析它并获得异常:

04-08 21:25:03.561: W/System.err(13464):        org.json.JSONException: No value for companyTopStatements
04-08 21:25:03.561: W/System.err(13464): at org.json.JSONObject.get(JSONObject.java:354)
04-08 21:25:03.561: W/System.err(13464): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
04-08 21:25:03.561: W/System.err(13464): at com.moodwonder.fi.HTTP_SignIn_Thread.doInBackground(HTTP_SignIn_Thread.java:105)
04-08 21:25:03.561: W/System.err(13464): at com.moodwonder.fi.HTTP_SignIn_Thread.doInBackground(HTTP_SignIn_Thread.java:1)
04-08 21:25:03.561: W/System.err(13464): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 21:25:03.561: W/System.err(13464): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-08 21:25:03.561: W/System.err(13464): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-08 21:25:03.561: W/System.err(13464): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-08 21:25:03.561: W/System.err(13464): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-08 21:25:03.566: W/System.err(13464): at java.lang.Thread.run(Thread.java:841)

最佳答案

JSONObject jsonObj = new JSONObject(Get_MyCompany_JSON);
JSONObject jsondata = jsonObj.getJSONObject("data");
JSONArray jsonArray = jsondata.getJSONArray("companyTopStatements");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject curr = jsonArray.getJSONObject(i);
String keyValue = curr.getString("key");
Log.d("Key",keyValue);
}

您忘记了 companyTopStatementsdata 对象中

关于android - 解析 JSONArray Android 中的 JSONObject 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22942744/

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