[英]Error Parsing JSON file in Android
I am developing an Android application which fetches data from a server through HTTP GET
, stores the response ( JSON
format) in a String. 我正在开发一个Android应用程序,该应用程序通过HTTP GET
从服务器获取数据,并将响应( JSON
格式)存储在String中。 I want to parse the String and get the values within the JSONArray
. 我想解析String并获取JSONArray
的值。
The received JSON which is stored in the STRING is : 存储在STRING中的接收到的JSON是:
{"code":1,"data":{"survey":{"id":null,"questionAnswers":[{"id":null,"min":"1","max":"5","step":"0.1","last":"4.7","text":"I am happy at work today","key":"Mood","testQuestionId":"1","datetime":"2014-04-29 15:47:02","answer":"4.7","testAnswerId":null}]}}
I want to parse the JSON Array "questionAnswers"
, and retrieve the value in "answer"
, which is inside a JSONObject
. 我想解析JSON数组"questionAnswers"
,并在JSONObject
内部的"answer"
检索值。
The code I used is as follows : 我使用的代码如下:
JSONObject jsonObj_survey = new JSONObject(Get_MySurvey_JSON);
JSONObject jsondata_survey = jsonObj_survey.getJSONObject("data");
JSONArray jsonArray_questionAnswer = jsondata_survey
.getJSONArray("questionAnswers");
for (int i = 0; i < jsonArray_questionAnswer.length(); i++) {
JSONObject questionAnswer = jsonArray_questionAnswer
.getJSONObject(i);
my_Company_survey_rating[i] = questionAnswer.getString("answer");
// userdata.setMy_Company_Top_key_1(my_Company_Top_key[1]);
Log.d("Rating", my_Company_survey_rating[i]);
}
I am unable to parse it and obtaining an exception : 我无法解析它并获得异常:
04-29 18:28:00.272: W/System.err(13189): org.json.JSONException: No value for questionAnswers
04-29 18:28:00.272: W/System.err(13189): at org.json.JSONObject.get(JSONObject.java:354)
04-29 18:28:00.272: W/System.err(13189): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
04-29 18:28:00.272: W/System.err(13189): at com.moodwonder.fi.HTTP_SignIn_Thread.doInBackground(HTTP_SignIn_Thread.java:224)
04-29 18:28:00.272: W/System.err(13189): at com.moodwonder.fi.HTTP_SignIn_Thread.doInBackground(HTTP_SignIn_Thread.java:1)
04-29 18:28:00.272: W/System.err(13189): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-29 18:28:00.272: W/System.err(13189): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-29 18:28:00.272: W/System.err(13189): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-29 18:28:00.272: W/System.err(13189): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-29 18:28:00.272: W/System.err(13189): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-29 18:28:00.272: W/System.err(13189): at java.lang.Thread.run(Thread.java:841)
Try this code - 试试这个代码-
JSONObject jsonObj_survey = new JSONObject(Get_MySurvey_JSON);
JSONObject jsondata_survey = jsonObj_survey.getJSONObject("data");
JSONObject jsondata_surveyvalue = jsonObj_survey.getJSONObject("survey");
JSONArray jsonArray_questionAnswer = jsondata_surveyvalue
.getJSONArray("questionAnswers");
for (int i = 0; i < jsonArray_questionAnswer.length(); i++) {
JSONObject questionAnswer = jsonArray_questionAnswer
.getJSONObject(i);
my_Company_survey_rating[i] = questionAnswer.getString("answer");
// userdata.setMy_Company_Top_key_1(my_Company_Top_key[1]);
Log.d("Rating", my_Company_survey_rating[i]);
}
Hope this code helps you!! 希望这段代码对您有帮助!! is it is not working please let me know i will try to help more. 是行不通的,请让我知道我会尝试提供更多帮助。
You are trying to get JSONArray of "questionAnswers" from "data" instead of "survey" 您正在尝试从“数据”而不是“调查”中获取“ questionAnswers”的 JSONArray
String yourJSONString = "{"code":1,"data":{"survey":{"id":null,"questionAnswers":[{"id":null,"min":"1","max":"5","step":"0.1","last":"4.7","text":"I am happy at work today","key":"Mood","testQuestionId":"1","datetime":"2014-04-29 15:47:02","answer":"4.7","testAnswerId":null}]}}}";
JSONObject jObj_main = new JSONObject(yourJSONString);
JSONObject jOb_data = jObj_main.getJSONObject("data");
JSONObject jOb_survey = jOb_data.getJSONObject("survey");
JSONArray jArray_questionAnswers = jOb_survey.getJSONArray("questionAnswers");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.