JSON object analysis + array in Java

I am trying to parse the following JSON:

{"city":{"id":2643743,"name":"London","coord":{"lon":-0.12574,"lat":51.50853},"country":"GB","population":0},"cod":"200","message":0.0456,"cnt":7,"list":[

    {"dt":1440504000,"temp":
        {"day":16.85,"min":14.23,"max":16.85,"night":14.23,"eve":16.32,"morn":16.85},
    "pressure":1013.06,"humidity":79,"weather":[
        {"id":500,"main":"Rain","description":"light rain","icon":"10d"}],
    "speed":7.36,"deg":172,"clouds":88,"rain":1.09},
    {"dt":1440504001,"temp":
        {"day":16.85,"min":10.03,"max":18.15,"night":14.23,"eve":16.32,"morn":16.85},
    "pressure":1013.06,"humidity":45,"weather":[
        {"id":501,"main":"Rain","description":"light rain","icon":"10d"}],
    "speed":15.46,"deg":172,"clouds":88,"rain":1.09},
    {"dt":1440504002,"temp":
        {"day":16.85,"min":4.73,"max":11.12,"night":14.23,"eve":16.32,"morn":16.85},
    "pressure":1013.06,"humidity":59,"weather":[
        {"id":502,"main":"Rain","description":"light rain","icon":"10d"}],
    "speed":17.12,"deg":172,"clouds":88,"rain":1.09}]}

The information I want to parse is “min,” “max,” “humidity,” and “speed.” Since this is the first time I have to parse arrays, I don’t know exactly how to parse it. After reading some forum topics, I wrote the following code:

public void filtraOW7days(String contenidoOW) throws ParseException{ 
    JSONParser parser = new JSONParser();
    try{
        Object obj = parser.parse(contenidoOW);
        JSONObject jsonList = (JSONObject) obj;
        JSONArray list = (JSONArray) jsonList.get("list");
        Iterator<String> unitsIterator = list.iterator();
        int i = 0;
        while(unitsIterator.hasNext()){
            Object uJson = unitsIterator.next();
            JSONObject uj = (JSONObject) uJson;
            this.humOWaux[i] =  (long) uj.get("humidity");
            this.windOWaux[i] = (String) uj.get("speed");


            JSONArray slideContent = (JSONArray) uj.get("temp");
            Iterator c = slideContent.iterator();


            while (c.hasNext()) {
                JSONObject slide = (JSONObject) c.next();
                this.tmaxOWaux[i] =(String) slide.get("max"); 
                this.tminOWaux[i] = (String) slide.get("min"); 
            }


            i++;
        }

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

}

Where String contenidoOW is JSON. This code gives me the following error:

java.lang.NullPointerException in "this.humOWaux [i] = (long) uj.get (" humidity ");".

I don’t understand why (at least I don’t understand why), so please help me?

EDIT: I use org.json.simple and variable declaration:

private String[] tminOWaux; private String[] tmaxOWaux; private long[] humOWaux; private String[] windOWaux;

Thanks for your time, really appreciate it!

+4
2
Long humidity=null; // long humidity=0;
if(uj.get("humidity")!=null){
  humidity=Long.valueOf(uj.get("humidity"))
}
0

, , , . :

//Using org.json.simple
private double[] tminOWaux = new double[7];
private double[] tmaxOWaux = new double[7];
private long[] humOWaux = new long[7];
private double[] windOWaux = new double[7]; 
public void filtraOW7days(String contenidoOW) throws ParseException{ 
    JSONParser parser = new JSONParser();
    try{
        Object obj = parser.parse(contenidoOW);
        JSONObject jsonList = (JSONObject) obj;
        JSONArray list = (JSONArray) jsonList.get("list");
        Iterator<String> unitsIterator = list.iterator();
        int i = 0;
        while(unitsIterator.hasNext()){
            Object uJson = unitsIterator.next();
            JSONObject uj = (JSONObject) uJson;
            this.humOWaux[i] =  (long) uj.get("humidity");
            this.windOWaux[i] = (double) uj.get("speed");

            JSONObject slideContent = (JSONObject) uj.get("temp");
            this.tmaxOWaux[i] = (double) slideContent.get("max");
            this.tminOWaux[i] = (double) slideContent.get("min");

            i++;
        }

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

, , . , JSONObject JSONArray.

, , !

0

All Articles