我正在为医疗应用程序创建一个 mcq,并且我正在尝试使用此 JSON 的不同选择从我的数据库中获取一些问题:
{
"QCM": [{
"question": "Est-ce que Guillaume a pris?",
"id": "34",
"choix": ["Oui", "Non"]
}]
}
然后我将问题字符串传递到 TextView ,如果我有 2 个选择,我只创建 2 个按钮,但只创建 1 个按钮,并且在我的按钮中我有这个字符串:
["Oui", "Non"]
所以我不明白,因为我为它创建了第二个 JSONArray 循环......
这是我的 Java
try
{
JSONArray QCM = response.getJSONArray("QCM");
for (int i=0; i<QCM.length(); i++) {
JSONObject getQcmObject = QCM.getJSONObject(i);
String questionGet = getQcmObject.getString("question");
symptomesQuestions.setText(questionGet);
for (int x=0; x<QCM.length(); x++){
JSONObject getChoixObject = QCM.getJSONObject(x);
String choiceGet = getChoixObject.getString("choix");
lesChoixButton.setText(choiceGet);
}
}
如果有人能告诉我怎么做,我想学习!找不到此类请求的任何示例。 谢谢大家!
最佳答案
你使用了错误的解析器,像这样改变它:
JSONArray QCM = response.getJSONArray("QCM");
for (int i = 0; i < QCM.length(); i++) {
JSONObject getQcmObject = QCM.getJSONObject(i);
String questionGet = getQcmObject.getString("question");
symptomesQuestions.setText(questionGet);
JSONArray choiceGet = getChoixObject.getJSONArray("choix");
lesChoixButton1.setText(choiceGet.getString(0));
lesChoixButton2.setText(choiceGet.getString(1));
}
使用此站点从您的 json 创建 java pojo 模型:http://www.jsonschema2pojo.org/
关于java - 如何解析 JSON 中的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955799/