我正在开发一个Android应用程序,该应用程序通过HTTP GET从服务器获取数据,并将响应(JSON格式)存储在String中。我想解析String并获取JSONArray中的值。
存储在STRING中的接收到的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”,并检索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)
最佳答案
org.json.JSONException:companyTopStatements没有值
您在顶部有一个json对象节点,但JSONArray companyTopStatements
不在该json对象下。
{ // json object node
"code": 1,
"data": { // json object data
根据数据
"companyTopStatements": [
您在JSONObject
companyTopStatements
下有JSONArray data
。您可以通过粘贴json @进行确认
http://www.jquery4u.com/demos/online-json-tree-viewer/#
JSONObject jsonObj = new JSONObject(Get_MyCompany_JSON);
JSONObject jb = jsonObj.getJSONObject("data");
JSONArray jsonArray = jb.getJSONArray("companyTopStatements");