从阅读JSON在Android [英] Reading from JSON on Android
本文介绍了从阅读JSON在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了JSON格式的字符串。它看起来像:
I have created an string in JSON format. It looks like:
private String jString = "{\"CategoryId\":1},{\"CategoryId\":2}";
,然后这个被初始化为JSON对象是这样的:
and this is then initialized as an JSON object like this:
JSONObject jObject = new JSONObject(jString);
阅读我用时:
j = jObject.getString("CategoryId");
我可以阅读的第一个值,但我不能读第二个。如何解决任何想法?
I can read the first value but I can't read the second one. Any ideas on how to solve?
推荐答案
这是不是一个有效的JSON格式。 看这里看到一个JSON对象/数组的格式。
This is not a valid JSON format. Look here to see the format of a JSON object / array.
如果您希望能够阅读这两个值,创建一个数组:
If you want to be able to read both values, create an array:
String jString = "[{\"CategoryId\":1},{\"CategoryId\":2}]";
JSONArray jArr = new JSONArray(jString);
for (int i = 0; i < jArr.length(); ++i) {
int i = jArr.getJsonObject(i).getInt("CategoryId");
// do something with i which is an int, not a String
}
这篇关于从阅读JSON在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文