2017-02-11 26 views
0

erhalten JSONObject von URL-Json Quelle.JSON Parsing Daten von URL NullpunktException

public class source02 { 
     public static void main(String[] args) { 
      try { 
      URL url = new URL("http://openapi.seoul.go.kr:8088/sample/json/StationDayTrnsitNmpr/1/5/"); 
      InputStreamReader isr = new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"); 
      JSONObject object = (JSONObject)JSONValue.parse(isr); 

      JSONObject sdt = (JSONObject) object.get("StationDayTrnsitNmpr"); 
      System.out.println(sdt.get("list_total_count").toString()); 

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

} 

und Json Quelle

{"StationDayTrnsitNmpr":{"list_total_count":44,"RESULT":{"CODE":"INFO-000","MESSAGE":"정상 처리되었습니다"},"row":[{"SN":"1","STATN_NM":"신도림","WKDAY":333873.0,"SATDAY":298987.0,"SUNDAY":216886.0},{"SN":"2","STATN_NM":"동대문역사문화공원","WKDAY":251049.0,"SATDAY":211456.0,"SUNDAY":150589.0},{"SN":"3","STATN_NM":"충무로","WKDAY":229882.0,"SATDAY":194865.0,"SUNDAY":142150.0},{"SN":"4","STATN_NM":"종로3가","WKDAY":224539.0,"SATDAY":196606.0,"SUNDAY":142525.0},{"SN":"5","STATN_NM":"사당","WKDAY":200985.0,"SATDAY":180230.0,"SUNDAY":134354.0}]}} 

bekommen java.lang.NullPointerException bei api.source02.main (source02.java:16)

+0

Was ist genau die Linie 16? Protokollieren Sie auch das Objekt, um zu sehen, wie es dargestellt wird. –

Antwort

0

Gut, das ist für mich arbeiten

 URL url = new URL("http://openapi.seoul.go.kr:8088/sample/json/StationDayTrnsitNmpr/1/5/"); 
     InputStreamReader isr = new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"); 
     BufferedReader br = new BufferedReader(isr); 
     StringBuilder response = new StringBuilder(); 
     for (String line = br.readLine(); line != null; line = br.readLine()) { 
      response.append(line); 
     } 
     JSONObject object = new JSONObject(response.toString()); 
     JSONObject sdt = (JSONObject) object.get("StationDayTrnsitNmpr"); 
     System.out.println(sdt.get("list_total_count").toString()); 
+0

eine weitere Frage. Wie mache ich eine "Zeile" JSON Array Liste? Wie greife ich von JSonObject sdt auf die "Zeile" zu? –

+0

@JakeAvacado Ich verstehe es nicht. Was willst du erreichen? –

+0

@JakeAvacado dies funktioniert - 'System.out.println (sdt.get (" row "));' –