首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android-解析本地JSON数据(资产文件夹)

Android-解析本地JSON数据(资产文件夹)
EN

Stack Overflow用户
提问于 2015-04-29 11:16:32
回答 1查看 5.9K关注 0票数 1

我正在用Android做一个测试应用。我有一个json文件,我想把它读到活动类中。这是json文件。编写了代码,但代码没有识别我所描述的“问题”变量。

代码语言:javascript
复制
{
    "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部分。

代码语言:javascript
复制
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文件之后(应该有一个逗号我还没有注意到,我就必须用一个字符(像a200)来修改被描述为int并在json文件中给出的id值。在那之后,问题解决了,json也被抓走了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-29 11:18:10

创建一个新的JSONObject,而不是解析您的文件:

代码语言:javascript
复制
JSONObject jsonObjMain = new JSONObject();

应:

代码语言:javascript
复制
JSONObject jsonObjMain = new JSONObject(sb.toString());

编辑:

现在我明白你的问题了。

您已经超出了范围,并且变量question不再存在。还有多重问题,您希望如何将其设置为您的TextView

您可以将问题保存到数组中,并像这样使用它们中的任何一个:

代码语言:javascript
复制
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));
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29942090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档