开发者

How to select data from this json using JSONObject

开发者 https://www.devze.com 2023-02-19 11:52 出处:网络
{ \"antikguiden\": { \"stores\":[ 开发者_开发技巧{ \"name\": \"Ninas Bruk & Brocante (Fd Isakssons Antik och Kuriosa)\",
{
 "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.

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/

But, yeah, Matt is right, it's not a valid JSON


OK. I don't think you posted the full JSON string;

Below is a valid JSON string:

{ "antikguiden": 
    { "stores":[
        {"name": "Ninas Bruk & Brocante (Fd Isakssons Antik och Kuriosa)","category": "Antikaffär"}
      ]}
}

Below is how to parse it in 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消