简体   繁体   中英

How to get nested arrays with JSONObject (org.json)

this is an example of my JSON-file:

{

    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "KKOD": 414,
                "KATEGORI": "Kommun",
                "KOMMUNKOD": 2584,
                "KOMMUNNAMN": "Kiruna",
                "LANSKOD": 25,
                "LANSNAMN": "Norrbottens län",
                "KOM_KOD": "2584",
                "LAN_KOD": "25"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            20.468899715356947,
                            69.0576379270828
                        ],
                        [
                            20.54863836554442,
                            69.05997605732921
                        ]
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "KKOD": 414,
                "KATEGORI": "Kommun",
                "KOMMUNKOD": 1262,
                "KOMMUNNAMN": "Lomma",
                "LANSKOD": 12,
                "LANSNAMN": "Skåne län",
                "KOM_KOD": "1262",
                "LAN_KOD": "12"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            13.11196493557692,
                            55.702721301997265
                        ],
                        [
                            13.112159474347964,
                            55.69989518845077
                        ],
                        [
                            13.111027902960512,
                            55.69899875723693
                        ]
                    ]
                ]
            }
        }
    ]

}

I would like to get the array of coordinates parsed to either a Double or String array in Java. This is how I managed to get the String from the property "KOMMUNNAMN":

JSONObject json = new JSONObject(readInput()); //readInput() returns the JSON document as String
JSONArray jsonarr = json.getJSONArray("features");
json = jsonarr.getJSONObject(0);
json = json.getJSONObject("properties");
String namn = json.getString("KOMMUNNAMN");

How do I proceed from here? Thanks!

Get the String inside the JSONObject by using for loop and stored it in the ArrayList

ArrayList<String> name=new ArrayList<String>();
JSONObject json = new JSONObject(readInput()); //readInput() returns the JSON document as String
JSONArray jsonarr = json.getJSONArray("features");
json = jsonarr.getJSONObject(0);
json = json.getJSONObject("properties");
for(int i=0;i<=json.length;i++){
name.add(json.getString("KOMMUNNAMN"));
}

Your coordinates entity is unnecessarily nested and does not read well. Change it to

"coordinates": [
        {
            "latitude": 13.11196493557692,
            "longitude": 55.702721301997265
        },
        {
            "latitude": 13.112159474347964,
            "longitude": 55.69989518845077
        },
        {
            "latitude": 13.111027902960512,
            "longitude": 55.69899875723693
        }
    ]

This reads as "coordinates contains an array of location objects" which indicates a better JSON structure.

You should now parse the coordinates with the following code:

JSONObject json = new JSONObject(readInput()); //readInput() returns the JSON document as String
JSONArray jsonarr = json.getJSONArray("features");
json = jsonarr.getJSONObject(0);
json = json.getJSONObject("geometry");
jsonarr = json.getJSONArray("coordinates");

for(int i=0;i<jsonarr.length();i++){
    JSONObject location = jsonarr.getJSONObject(i);
    String latitude = location.getString("latitude");
    String longitude = location.getString("longitude");
}

Also, I don't know if this is your final code or a subset but make sure you parse defensively; It's better to check length of arrays and whether an object is null than to throw and handle a JSONException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM