[英]How to select data from this json using JSONObject
{
"antikguiden": {
"stores":[
{
"name": "Ninas Bruk & Brocante (Fd Isakssons Antik och Kuriosa)",
"category": "Antikaffär" },}
Now i want to select the data name & category 现在我要选择数据名称和类别
to show on Android Emulator. 在Android模拟器上显示。
How can I write the code? 如何编写代码? Thanks.
谢谢。
I think from here you can learn by example http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/ 我认为您可以从这里通过示例学习http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
But, yeah, Matt is right, it's not a valid JSON 但是,是的,Matt是对的,它不是有效的JSON
OK. 好。 I don't think you posted the full JSON string;
我不认为您发布了完整的JSON字符串;
Below is a valid JSON string: 以下是有效的JSON字符串:
{ "antikguiden":
{ "stores":[
{"name": "Ninas Bruk & Brocante (Fd Isakssons Antik och Kuriosa)","category": "Antikaffär"}
]}
}
Below is how to parse it in Android: 以下是在Android中解析它的方法:
String jsonStr = the json string above;
JSONObject jObject = new JSONObject(jsonStr);
JSONObject antikguiden = jObject.getJSONObject("antikguiden");
JSONArray storeList = antikguiden.getJSONArray("stores");
for(int i = 0; i < storeList.length(); i++) {
JSONObject story = storeList.getJSONObject(i);
String name = story.getString("name");
String category = story.getString("category");
}
I haven't tested it, so there might be a syntax error or two; 我还没有测试过,所以可能会出现一两个语法错误。 but that's how you do it.
但这就是你要做的。
If your string is like the one below. 如果您的字符串类似于下面的字符串。
str = "{'antikguiden':{'stores':[{'name': 'Ninas Bruk & Brocante (Fd Isakssons Antik och Kuriosa)','category': 'Antikaffär'}] }}"; try { JSONObject obj = new JSONObject(str); JSONObject obj1 = obj.getJSONObject("antikguiden"); JSONArray jsonarray = obj1.getJSONArray("stores"); Log.v("TEST","name is "+jsonarray.getJSONObject(0).get("name")); Log.v("TEST","category is "+jsonarray.getJSONObject(0).get("category")); } catch (JSONException e) { e.printStackTrace(); }
To make it generalized as James has mentioned , use a loop, after getting json array. 为了使它像James提到的那样通用,请在获取json数组后使用循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.