Android从JSON对象读取/解析数据

时间:2014-05-26 07:28:49

标签: java android json

我收到以下JSON对象:

{
  "12.9624669, 77.6381958": "Domlur, Bangalore, Bangalore Urban, Karnataka, India",
  "12.9612856, 77.6362465": "Domlur, Old Airport Road, Domlur, Bangalore, Bangalore Urban, Karnataka, 560071, India",
  "12.9614031, 77.636241": "Domlur, HAL Airport Road, Domlur, Bangalore, Bangalore Urban, Karnataka, 560071, India"
}

我想从这个对象中检索数据并将其存储在一个数组中。

我想只检索部分数据,即。只有“Domlur,Old Airport Road”或“Domlur,HAL Airport Road”,而不是整个字符串。

2 个答案:

答案 0 :(得分:0)

ArrayList<String> result = new ArrayList<String>();
try {
    JSONObject jObject = new JSONObject(json.trim());
    Iterator<?> keys = jObject.keys();
    while (keys.hasNext()) {
        String s = jObject.getString((String) keys.next());
        s = s.substring(0, s.indexOf(",", s.indexOf(",") + 1));
        result.add(s);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

您可以迭代JSON键并使用动态值及其内容,如下所示:

            JSONObject json = new JSONObject(jsonStr); //jsonStr is your JSON
            Iterator<String> iter = json.keys();                     
            while (iter.hasNext()) { 
                String key = iter.next();  //gets the dynamic key
                Log.v("printlog","Dynamic Gps key: "+key);
                try { 
                    String content=json.getString(key); // gets contents from the dynamic key
                    Log.v("printlog","Content: "+content);

                }catch (JSONException e) { }