2

I'm trying to parse a json file which is this :

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

this is what i've managed to do

    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"));

The problem is about the lng and lat.

Anyone have a idea how about how to resolve this?

1
  • hi the descriptive answer is given please have a look!! Commented May 12, 2014 at 9:40

2 Answers 2

4

Use the below code.

 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"));
Sign up to request clarification or add additional context in comments.

Comments

0

as yours json is in given format

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

so parse as follows

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();
        }

so finally in yours code what you can say is

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.