JSON object verwerken in Java

Status
Niet open voor verdere reacties.

panzerfaustnl

Nieuwe gebruiker
Lid geworden
26 feb 2015
Berichten
3
Hallo allemaal,

Ik zit al een flinke avond te knoeien met een leuke opdracht,

Ik heb een JSONObject met de volgende vulling:

Code:
{"destinations":[{"origin":"AMS","destination":{"code":"AAL","name":"Aalborg","description":"Aalborg (AAL)","country":"DK","continent":"EUR","coordinates":{"lat":57.08944,"lon":9.84889}},"lowestFare":{"value":149.0,"currency":"EUR"}}

Maar op coordinates en lowestFare, loopt de volgende code niet:

Code:
public void getDestinations() {
        RestTemplate restTemplate = new RestTemplate();
        List<KLM> klmApp = new ListImpl();
        JSONObject jsonResult = null;
        JSONArray arrayResult = null;
        ArrayList<String> listObjects = null;
        try {

            HttpEntity<?> responseEntity = restTemplate.getForEntity(url, String.class);
            System.out.println(responseEntity.getBody());
            JSONObject json = (JSONObject) JSONValue.parse(responseEntity.getBody().toString());
            List<JSONObject> list = (List<JSONObject>) json.get("destinations");
            if (list != null)
            {
                for (JSONObject s : list) 
                {
                    System.out.println(s.toJSONString());
                    JSONParser parser = new JSONParser();
                    try
                    {
                        Object obj = parser.parse(s.toJSONString());
                        JSONObject jsonObject = (JSONObject)obj;
                        HashMap<String,String> destination = (HashMap<String,String>)jsonObject.get("destination");
                        System.out.println("==========================");
                        System.out.println(destination.get("continent"));
                        System.out.println(destination.get("country"));
                        System.out.println(destination.get("code"));
                        System.out.println(destination.get("name"));
                        System.out.println(destination.get("coordinates"));
                        System.out.println(destination.get("description"));
                        System.out.println(destination.get("lowestFare"));
                        System.out.println(destination.get("currency"));
                        System.out.println(destination.get("value"));
                        System.out.println("==========================");
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                        System.out.println("==========================");
                    }
                } 
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

iemand een idee waarom dit niet loopt? Ik heb al allerlei dingen geprobeerd met JSONArray's etc. Maar echt verder kom ik niet.
 
coordinates en lowestfare zijn subarrays welke in destination zitten, deze kan je volgens mij niet zo direct benaderen. Dan moet je de velden van de subarrays gebruiken.
 
dank voor je antwoord,

Ik ben inmiddels wat verder gekomen, ik kan nu lowestFare eruit krijgen, maar coordinates niet.

Code:
   @Test
    @JsonIgnoreProperties(ignoreUnknown = true)
    public void getDestinations() {
        RestTemplate restTemplate = new RestTemplate();
        List<KLM> klmApp = new ListImpl();
        JSONObject jsonResult = null;
        JSONArray arrayResult = null;
        ArrayList<String> listObjects = null;
        try 
        {

            HttpEntity<?> responseEntity = restTemplate.getForEntity(url, String.class);
            System.out.println(responseEntity.getBody());
            JSONObject json = (JSONObject) JSONValue.parse(responseEntity.getBody().toString());
            List<JSONObject> list = (List<JSONObject>) json.get("destinations");
            if (list != null)
            {
                for (JSONObject s : list) 
                {
                    System.out.println(s.toJSONString());
                    JSONParser parser = new JSONParser();
                    try
                    {
                        Object obj = parser.parse(s.toJSONString());
                        JSONObject array = (JSONObject)obj;
                        System.out.println(array.get("origin"));
//                        System.out.println(array.get("destination"));
                        HashMap<String,String> destination = (HashMap<String,String>)array.get("destination");
                        System.out.println(destination.get("continent"));
                        System.out.println(destination.get("country"));
                        System.out.println(destination.get("code"));
                        System.out.println(destination.get("name"));
                        destination.get("coordinates");
                        System.out.println(destination.get("description"));
                        HashMap<String,String> lowestFare = (HashMap<String,String>)array.get("lowestFare");
                        System.out.println(lowestFare.get("currency"));
                        System.out.println(String.valueOf(lowestFare.get("value")));                        
//                        System.out.println(array.get("continent"));
                        
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                        System.out.println("==========================");
                    }
                } 
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Het blijft een raar ding dat joon.

Je hebt op een gegeven moment een HashMap destination en daar zit coordinates als node in met als key coordinates en als value weer een JSONObject

JSON object in een HashMap by Marco de Boer, on Flickr
 
Laatst bewerkt:
Gevonden, ik kon dat deel als een JSONObject eruit halen en dan opnieuw uitlezen.

Code:
 @Test
    @JsonIgnoreProperties(ignoreUnknown = true)
    public void getDestinations() {
        RestTemplate restTemplate = new RestTemplate();
//        List<KLM> klmApp = new ListImpl();
        JSONObject jsonResult = null;
        JSONArray arrayResult = null;
        ArrayList<String> listObjects = null;
        try 
        {

            HttpEntity<?> responseEntity = restTemplate.getForEntity(url, String.class);
            System.out.println(responseEntity.getBody());
            JSONObject json = (JSONObject) JSONValue.parse(responseEntity.getBody().toString());
            List<JSONObject> list = (List<JSONObject>) json.get("destinations");
            if (list != null)
            {
                for (JSONObject s : list) 
                {
                    System.out.println(s.toJSONString());
                    JSONParser parser = new JSONParser();
                    try
                    {
                        Object obj = parser.parse(s.toJSONString());
                        JSONObject array = (JSONObject)obj;
                        String origin = (String)array.get("origin");
                        HashMap<String,String> destination = (HashMap<String,String>)array.get("destination");
                        String continent = (String)destination.get("continent");
                        String country = (String)destination.get("country");
                        String code = (String)destination.get("code");
                        String name = (String)destination.get("name");
                        JSONObject obj2 = (JSONObject)array.get("destination");
                        JSONObject get4 = (JSONObject)obj2.get("coordinates");
                        String lon = String.valueOf(get4.get("lon"));
                        String lat = String.valueOf(get4.get("lat"));                       
                        String description = (String)destination.get("description");
                        HashMap<String,String> lowestFare = (HashMap<String,String>)array.get("lowestFare");
                        String currency = (String)lowestFare.get("currency");
                        String value = String.valueOf(lowestFare.get("value"));  
                        KLMManage manage = new KLMManage();
                        manage.addKLM(origin, code, name, description, country, continent, lon, lat, value, currency);
                        
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                        System.out.println("==========================");
                    }
                } 
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan