我正在使用Android开发一个测试应用。我有一个json文件,我想将其读入活动类。这是json文件。编写了代码,但是代码无法识别我所描述的“问题”变量。
{
"questions": [
{
"id": "a200",
"question": "lorem ipsum dolor _______ amet",
"optionA": "lorem",
"optionB": "ipsum",
"optionC" : "dolor",
"optionD": "sit"
"rightAnswer" : "sit"
},
{
"id": "b200",
"question": "_____ ipsum dolor sit amet",
"optionA": "lorem",
"optionB": "ipsum",
"optionC" : "dolor",
"optionD": "sit"
"rightAnswer" : "lorem"
},
{
"id": "c200",
"question": "lorem _____ dolor sit amet",
"optionA": "lorem",
"optionB": "ipsum",
"optionC" : "dolor",
"optionD": "sit"
"rightAnswer" : "ipsum"
},
]
}
这是我的QuizActivityAdjectives.java文件的onCreate部分。
public class QuizActivityAdjectives extends Activity {
TextView txt_questionAdjective;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_adjectives);
// Hangi xml dosyasının dikkate alınacağı belirlendi.
// Reading json file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"Questions.json")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
// Try to parse JSON
try {
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject();
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("questions");
// JSONArray has four JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
int id = jsonObj.getInt("id");
String question = jsonObj.getString("question");
String optionA = jsonObj.getString("optionA");
String optionB = jsonObj.getString("optionB");
String optionC = jsonObj.getString("optionC");
String optionD = jsonObj.getString("optionD");
String rightAnswer = jsonObj.getString("rightAnswer");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String myjsonstring = sb.toString();
txt_questionAdjective = (TextView) findViewById(R.id.txt_questionAdjective);
txt_questionAdjective.setText(question); //THE CODE ERRORS HERE. CAN NOT RESOLVE QUESTION VARIABLE.
}
我该如何摆脱这个问题?这样的代码有意义吗?能行吗?
错误:“问题无法解析为变量”(我在底部写的注释行)
编辑:解决了我永远都不会想到的问题,但是在纠正了json文件之后(应该是一个逗号,我没有注意到,我得更改被描述为int并在json文件中给出的id值)一个字符(如a200),此后问题就消失了,并提取了json。
您创建一个新的JSONObject,而不是解析您的文件:
JSONObject jsonObjMain = new JSONObject();
应该:
JSONObject jsonObjMain = new JSONObject(sb.toString());
编辑:
现在我明白了你的问题。
您超出范围,并且变量question
不再存在。此外,还有多个问题,您如何期望将其设置为您的TextView
?
您可以将问题保存到数组中,并像下面这样使用它们中的任何一个:
List<String> questions = new ArrayList<>();
try {
...
for (int i = 0; i < jsonArray.length(); i++) {
...
questions.add(jsonObj.getString("question"));
...
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String myjsonstring = sb.toString();
// Check if questions have been fetched
if (questions.size() > 0) {
txt_questionAdjective = (TextView) findViewById(R.id.txt_questionAdjective);
// Use the first question
txt_questionAdjective.setText(questions.get(0));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句