我如何从json解析数组中的数组中的对象

时间:2018-03-26 23:06:48

标签: android arrays json

我一直在尝试解析来自此对象的对象,该对象位于另一个数组内的数组中。我做了两个循环,因为有多个对象我正在尝试从中创建列表。

这就是json的样子

{
    "type": "FeatureCollection",
    "metadata": {
        "generated": 1522105396000,
        "url": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson",
        "title": "USGS Magnitude 4.5+ Earthquakes, Past Day",
        "status": 200,
        "api": "1.5.8",
        "count": 7
    },
    "features": [{
            "type": "Feature",
            "properties": {
                "mag": 4.7,
                "place": "106km SW of Hihifo, Tonga",
                "time": 1522100348420,
                "updated": 1522102052040,
                "tz": -720,
                "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us1000d9c6",
                "detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/us1000d9c6.geojson",
                "felt": null,
                "cdi": null,
                "mmi": null,
                "alert": null,
                "status": "reviewed",
                "tsunami": 0,
                "sig": 340,
                "net": "us",
                "code": "1000d9c6",
                "ids": ",us1000d9c6,",
                "sources": ",us,",
                "types": ",geoserve,origin,phase-data,",
                "nst": null,
                "dmin": 3.738,
                "rms": 0.85,
                "gap": 66,
                "magType": "mb",
                "type": "earthquake",
                "title": "M 4.7 - 106km SW of Hihifo, Tonga"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-174.4796, -16.604, 175.39]
            },
            "id": "us1000d9c6"
        },
        {
            "type": "Feature",
            "properties": {
                "mag": 4.8,
                "place": "West Chile Rise",
                "time": 1522025174820,
                "updated": 1522026587040,
                "tz": -360,
                "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us1000d90k",
                "detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/us1000d90k.geojson",
                "felt": null,
                "cdi": null,
                "mmi": null,
                "alert": null,
                "status": "reviewed",
                "tsunami": 0,
                "sig": 354,
                "net": "us",
                "code": "1000d90k",
                "ids": ",us1000d90k,",
                "sources": ",us,",
                "types": ",geoserve,origin,phase-data,",
                "nst": null,
                "dmin": 9.665,
                "rms": 0.87,
                "gap": 192,
                "magType": "mb",
                "type": "earthquake",
                "title": "M 4.8 - West Chile Rise"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-86.6644, -41.0452, 10]
            },
            "id": "us1000d90k"
        }
    ],
    "bbox": [-178.6721, -56.8886, 10, 151.396, 56.3364, 541.02]
}

我只需要来自两个阵列的placemagnitudetime,但我似乎无法选择正确的数组来获取对象。例如:(features.properties.place

            List<String> allPlaces = new ArrayList<>();
            JSONArray JA = new JSONArray(data);
            JSONArray features = JA.getJSONArray(2);
//          JSONArray properties = features.getJSONArray(1);
            for (int i = 0; i < features.length(); i++){
                JSONArray properties = (JSONArray) features.get(i);
                for (int j = 0; j < properties.length(); j++){
                    JSONObject JO = (JSONObject) features.get(i);
                    String place = JO.getString("place");
                    singlePlace = "place: " + place;
                    dataPlace = "" + singlePlace;
                    allPlaces.add(place);
                    }

edit1:要清楚,我能够从第一个数组获取信息,所以type的值,但不是功能内的任何内容,

1 个答案:

答案 0 :(得分:0)

问题是您要将属性解析为JSONArray,但实际上它是JSONObject。所以,你应该尝试这个循环来解析所需的数据:

    JSONObject root = new JSONObject(data); //Where `data` is your raw json string
    JSONArray features = root.getJSONArray("features");
    for (int i = 0; i < features.length(); i++) {
        JSONObject properties = features.getJSONObject(i).getJSONObject("properties");
        double mag = properties.getDouble("mag");
        String place = properties.getString("place");
        long time = properties.getLong("time");
    }