2 clcurie 11 clcurie_11 于 2013.07.17 17:55 提问

Android - json 解析的问题

我有如下的 JSON:

{
        "places": [{
            "name": "Ankola",
            "slug": "ankola",
            "category": "beach",
            "distance": "521",
            "travel_time": "8 hrs, 2 mins",
            "days": "3",
            "latitude": "14.669456",
            "longitude": "74.300952",
            "weather": "Summer 21\u00b0-36\u00b0C, Winter 12\u00b0-35\u00b0C",
            "todo": "Baskal gudda, Nadibag, Shedikuli, Keni, Belekeri",
            "about": "Ankola is a small town surrounded by numerous temples. It is in line with Arabian sea. Ankola is famous for its native breed of mango called ishaad and for cashews harvesting.",
           "image": [
                     "Cm5NXlq.jpg",
                     "9OrlQ9C.jpg",
                     "DRWZakh.jpg",
                     "dFKVgXA.jpg",
                     "5WO2nDf.jpg"
                     ]
        }]
}

我知道如何获取关键值- value pairs,但是我不知道如何在json中解析数组,再形成一个字符串数组。
我想实现的是,例如,我在 "image" tag下有5个图像名,我想让它们在一个字符串数组中,如何实现?

2个回答

cj6585256
cj6585256   2013.07.17 17:59

先把整的弄成一个JsonObject jo = new JsonObject(你获取的内容);
JsonArray ja = jo.optJsonArray("image");
后面再for循环

Jave_
Jave_   2013.07.18 09:46

楼上拼错了 =-=
完整的代码:

        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("places");
            for (int i = 0, l = jsonArray.length(); i < l; i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                JSONArray ja = jsonObject2.optJSONArray("image");
                String[] img = new String[5];
                for (int j = 0, k = ja.length(); j < k; j++) {
                    img[i] = ja.get(i).toString();
                    Log.v("", "" + img[i]);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
Csdn user default icon
上传中...
上传图片
插入图片