如何在JsonArray中解析JsonObject

时间:2014-05-12 09:16:41

标签: android json

我正在尝试解析一个json文件:

{
"number": 123,
"contract_name" : "London",
"name": "station",
"address": "address",
"position": {
"lat": 48.862993,
"lng": 2.344294
},
"banking": true,
"bonus": false,
"last_update": <timestamp>
}

这就是我设法做的事情

    s.setName(stationJson.optString("name"));
                    s.setTimestamp(stationJson.optString("last_update"));
                    s.setNumber(stationJson.optInt("number"));
                    s.setBonus(stationJson.optInt("bonus"));
                    s.setBank(stationJson.optInt("banking"));
                    s.setPosition(stationJson.getJSONObject("position"));
                    s.setLatitude(stationJson.optDouble("lat"));
                    s.setLongitude(stationJson.optDouble("lng"));

问题在于lng和lat。

任何人都知道如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

使用以下代码。

 s.setName(stationJson.optString("name"));
 s.setTimestamp(stationJson.optString("last_update"));
 s.setNumber(stationJson.optInt("number"));
 s.setBonus(stationJson.optInt("bonus"));
 s.setBank(stationJson.optInt("banking"));
 JSONObject positionJson = stationJson.getJSONObject("position");    
 s.setLatitude(positionJson.optDouble("lat"));
 s.setLongitude(positionJson.optDouble("lng"));

答案 1 :(得分:0)

因为你的json是给定的格式

{
"number": 123,
"contract_name" : "London",
"name": "station",
"address": "address",
"position": {
"lat": 48.862993,
"lng": 2.344294
},
"banking": true,
"bonus": false,
"last_update": <timestamp>
}

解析如下

String jsondata ="YOURS JSON DATA HERE";//ASSIGN OR LOAD YOURS JSON DATA HERE
        try {
            JSONObject initial=new JSONObject(jsondata);

            int number=initial.getInt("number");
            String  contract_name=initial.getString("contract_name");
            Log.d("contract_name", contract_name);
            String  name=initial.getString("name");
            Log.d("name", name);
            String  address=initial.getString("address");
            Log.d("address", address);

            String  lastUpdate=initial.getString("last_update");
            Log.d("lastUpdate", lastUpdate);
            Boolean bonus=initial.getBoolean("bonus");
            Log.d("bonus", bonus+"");
            Boolean banking=initial.getBoolean("banking");
            Log.d("banking", banking+"");

            JSONObject another=initial.getJSONObject("position");
            Double lat=another.getDouble("lat");
            Log.d("lat", lat+"");
            Double lng=another.getDouble("lng");
            Log.d("lng", lng+"");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

所以最后在你的代码中你可以说是

s.setName(stationJson.optString("name"));
 s.setTimestamp(stationJson.optString("last_update"));
 s.setNumber(stationJson.optInt("number"));
 s.setBonus(stationJson.optInt("bonus"));
 s.setBank(stationJson.optInt("banking"));
 //make another object as "position"is another object
 JSONObject positionJson = stationJson.getJSONObject("position");    
 s.setLatitude(positionJson.optDouble("lat"));
 s.setLongitude(positionJson.optDouble("lng"));