json解析来自url的数据获取nullpointException

时间:2017-02-11 09:40:37

标签: java json jsp parsing

从URL-Json源获取JSONObject。

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

}

和Json来源

   {"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}]}}

获取java.lang.NullPointerException     在api.source02.main(source02.java:16)

1 个答案:

答案 0 :(得分:0)

这对我有用

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