Android 无法访问JSON
我无法从文件中访问JSON。我的JSON文件内容是:Android 无法访问JSON,android,json,Android,Json,我无法从文件中访问JSON。我的JSON文件内容是: { "AuditQuestions": [{ "categoryName": "General", "QuestionList": [{ "quesId": "01", "type": "editText", "questText": "Inspected By",
{
"AuditQuestions": [{
"categoryName": "General",
"QuestionList": [{
"quesId": "01",
"type": "editText",
"questText": "Inspected By",
"answer": ""
},
{
"quesId": "02",
"type": "editText",
"questText": "This Audit Is For City:",
"answer": ""
},
{
"quesId": "03",
"type": "editText",
"questText": "This Audit Is For Property:",
"answer": ""
},
]
},
{
"categoryName": "Accessibility, Location and Security",
"QuestionList": [{
"quesId": "17",
"type": "radio",
"questText": "Could you find the property easily with the address given",
"answer": ["Yes", "No"]
},
{
"quesId": "18",
"type": "editText",
"questText": "Please suggest what we can add to the address to make it more locatable (eg: what landmark?)",
"answer": ""
},
]
}
.
.
.
.
.
.
}]
每个“问题列表”大约有10-15组,但为了简单起见,我跳过了它们
我访问它的代码是:
public void getQuestion(String page) {
questionList = new ArrayList<QuestionList>();
String category;
String quesId ;
String questionType ;
String questionText;
String answer;
try {
JSONObject jsonObj = new JSONObject(page);
JSONArray jssArray = jsonObj.getJSONArray("AuditQuestions");
int jsonArrLength = jssArray.length();
for (int i = 0; i < jsonArrLength; i++) {
JSONObject jsonChildObj = jssArray.getJSONObject(i);
category = jsonChildObj.getString("categoryName");
quesId = jsonChildObj.getJSONObject("QuestionList").getString("quesId");
questionType = jsonChildObj.getJSONObject("QuestionList").getString("type");
questionText = jsonChildObj.getJSONObject("QuestionList").getString("questText");
answer = jsonChildObj.getJSONObject("QuestionList").getString("answer");
QuestionList qList = new QuestionList();
qList.setQuesId(quesId);
qList.setQuesText(questionText);
qList.setQuesTypw(questionType);
qList.setAnswer(answer);
qList.setCategoryName(category);
questionList.add(qList);
}
.
.
.
.
.
}
public void getQuestion(字符串页){
questionList=新的ArrayList();
字符串类别;
字符串quesId;
字符串类型;
字符串文本;
字符串回答;
试一试{
JSONObject jsonObj=新JSONObject(第页);
JSONArray jssArray=jsonObj.getJSONArray(“审核问题”);
int jsonArrLength=jssArray.length();
for(int i=0;i
我试图找出我的代码是否有问题,但我觉得没问题。
如果我上面使用的代码有问题,请提供帮助。
提前谢谢!试试这个
for (int i = 0; i < jsonArrLength; i++) {
JSONObject jsonChildObj = jssArray.getJSONObject(i);
category = jsonChildObj.getString("categoryName");
JSONArray jsonArrayQueList = jsonChildObj.getJSONArray("QuestionList");
for(int j = 0 ; j<jsonArrayQueList.length(); j++)
{
JSONObject jsonObjectqueDetail = jsonArrayQueList.getJSONObject(j);
quesId = jsonObjectqueDetail.getString("quesId");
questionType = jsonObjectqueDetail.getString("type");
questionText = jsonObjectqueDetail.getString("questText");
answer = jsonObjectqueDetail.getString("answer");
QuestionList qList = new QuestionList();
qList.setQuesId(quesId);
qList.setQuesText(questionText);
qList.setQuesTypw(questionType);
qList.setAnswer(answer);
qList.setCategoryName(category);
questionList.add(qList);
}
}
for(int i=0;i 首先,您的json应该如下所示
{
"AuditQuestions":
[
{
"categoryName": "General",
"QuestionList": [
{
"quesId": "01",
"type": "editText",
"questText": "Inspected By",
"answer": ""
},
{
"quesId": "02",
"type": "editText",
"questText": "This Audit Is For City:",
"answer": ""
},
{
"quesId": "03",
"type": "editText",
"questText": "This Audit Is For Property:",
"answer": ""
}]
},
{
"categoryName": "Accessibility, Location and Security",
"QuestionList": [{
"quesId": "17",
"type": "radio",
"questText": "Could you find the property easily with the address given",
"answer": ["Yes", "No"]
},
{
"quesId": "18",
"type": "editText",
"questText": "Please suggest what we can add to the address to make it more locatable (eg: what landmark?)",
"answer": ""
}]
}]
}
然后像这样解析。我已经解析了它,并将您的问题列表对象放入其中
try {
JSONObject jsonObj = new JSONObject(input);
JSONArray jssArray = jsonObj.getJSONArray("AuditQuestions");
int jsonArrLength = jssArray.length();
for (int i = 0; i < jsonArrLength; i++) {
JSONObject jsonChildObj = jssArray.getJSONObject(i);
category = jsonChildObj.getString("categoryName");
JSONArray jssArray1 = jsonChildObj.getJSONArray("QuestionList");
for (int i1 = 0; i1 < jssArray1.length(); i1++) {
JSONObject jsonChildObj1 = jssArray1.getJSONObject(i1);
questionType = jsonChildObj1.getString("type");
questionText = jsonChildObj1.getString("questText");
answer = jsonChildObj1.getString("answer");
quesId = jsonChildObj1.getString("quesId");
QuestionList qList = new QuestionList();
qList.setQuesId(quesId);
qList.setQuesText(questionText);
qList.setQuesTypw(questionType);
qList.setAnswer(answer);
qList.setCategoryName(category);
questionList.add(qList);
}
}
}catch (JSONException e)
{
e.printStackTrace();
}
试试看{
JSONObject jsonObj=新的JSONObject(输入);
JSONArray jssArray=jsonObj.getJSONArray(“审核问题”);
int jsonArrLength=jssArray.length();
for(int i=0;i
首先,您要解析的json数据是错误的
尝试格式化json字符串,然后解析
尝试在jsonviewer中添加json数据并进行相应更改
jsonviewer链接如下所示:
JSON无效(},]
)。我正在尝试从android中的本地数据库访问JSON,该数据库可以动态放置在XML布局中。我只是想获得帮助,看看代码是否有任何问题。如果我的问题不清楚,我向您道歉。您是否尝试过…运行代码?是的,代码运行时没有任何错误或异常,但文件内容不会显示在输出中。如果您希望代码审查,请将其发布在,然后删除此问题。如果此代码存在真正的问题,请描述您面临的问题。我正在获得所有结果,但我不知道您的情况