-1

我正在阅读 JSON ReST API。和 JSON 看起来不同第一个看起来像这样:

processJSON({
  "TripList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestTrip.xsd",
  "servertime":"10:39",
  "serverdate":"2013-04-04",
  "Trip":[{
    "Leg":[{
      "name":"Buss 761",
      "type":"BUS",
      "id":"9015014576100035",
      "direction":"Heden via Linnéplatsen",
      "fgColor":"#00A5DC",
      "bgColor":"#ffffff",
      "stroke":"Solid",
      "Origin":{
        "name":"Hökegårdsgatan, Mölndal",
        "type":"ST",
        "id":"9022014012273002",
        "routeIdx":"30",
        "time":"10:42",
        "date":"2013-04-04",
        "track":"B ",
        "$":"\n"
        }

第二个是这样的:

processJSON({
"TripList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestTrip.xsd",
  "servertime":"10:46",
  "serverdate":"2013-04-04",
  "Trip":[{
    "Leg":{
      "name":"Spårvagn 8",
      "type":"TRAM",
      "id":"9015014500800075",
      "direction":"Angered",
      "fgColor":"#872387",
      "bgColor":"#ffffff",
      "stroke":"Solid",
      "accessibility":"wheelChair",
      "Origin":{
        "name":"Korsvägen, Göteborg",
        "type":"ST",
        "id":"9022014003980005",
        "routeIdx":"12",
        "time":"10:50",
        "date":"2013-04-04",
        "track":"E ",
        "rtTime":"10:51",
        "rtDate":"2013-04-04",
        "$":"\n"
        }

现在我想做的是如果 JSON“腿”是一个数组,我会循环到数组中并获取数据。但是如果“腿”是一个对象,那么循环就没有用了,我可以挖掘并获取数据。这个对我有用

但是第一个不起作用并给出了空指针期望。

我的代码是:

public class ParseTimeTable {

String temp = "";

public void parseJsonData() throws ParseException, IOException {

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(Planner.getPlanner().getJsonDataForTrip(
            Planner.getPlanner().getStartLocationID(),
            Planner.getPlanner().getDestinationID()));
    JSONObject topObject = (JSONObject) obj;
    JSONObject tripList = (JSONObject) topObject.get("TripList");
    JSONArray array = (JSONArray) tripList.get("Trip");
    Iterator<JSONObject> iterator = array.iterator();

    while (iterator.hasNext()) {

        JSONObject jsonObj = iterator.next();

        if (jsonObj.get("Leg") instanceof JSONArray)

        {

            JSONArray array2 = (JSONArray) tripList.get("Leg");
            Iterator<JSONObject> iterator2 = array2.iterator();
            while (iterator2.hasNext()) {

                String line = (String) jsonObj.get("name");
                String direction = (String) jsonObj.get("direction");
                JSONObject origin = (JSONObject) jsonObj.get("Origin");
                String name = (String) origin.get("name");
                String time = (String) origin.get("time");
                String track = (String) origin.get("track");

                JSONObject destination = (JSONObject) jsonObj.get("Destination");
                String dname = (String) destination.get("name");
                String dtime = (String) destination.get("time");
                String dtrack = (String) destination.get("track");

                temp = line + " " + "Mot : " + direction + " " + "Från : "
                        + name + " " + "Avgång : " + time + " "
                        + "Track : " + track + "- " + "Destination : "
                        + dname + " " + "Ankomst : " + dtime + " "
                        + "Track : " + dtrack;

                TripService.getTrip().setList(temp);

            }

        }

        else {

            jsonObj = (JSONObject) jsonObj.get("Leg");
            String line = (String) jsonObj.get("name");
            String direction = (String) jsonObj.get("direction");
            JSONObject origin = (JSONObject) jsonObj.get("Origin");
            String name = (String) origin.get("name");
            String time = (String) origin.get("time");
            String track = (String) origin.get("track");

            JSONObject destination = (JSONObject)                           jsonObj.get("Destination");
            String dname = (String) destination.get("name");
            String dtime = (String) destination.get("time");
            String dtrack = (String) destination.get("track");

            temp = line + " " + "Mot : " + direction + " " + "Från : "
                    + name + " " + "Avgång : " + time + " " + "Track : "
                    + track + "- " + "Destination : " + dname + " "
                    + "Ankomst : " + dtime + " " + "Track : " + dtrack;

            TripService.getTrip().setList(temp);

        }

    }

}

}

因此,我的 else 语句中的所有内容都按应有的方式工作,但在我的 if 语句中,我得到了一个空指针,因为 array2 是空的。所以给我空指针的是我的第二个数组: JSONArray array2 = (JSONArray) tripList.get("腿”);

为什么会这样?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at se.lowdin.trafficPlanner.ParseTimeTable.parseJsonData(ParseTimeTable.java:35)
    at se.lowdin.trafficPlanner.MainWindow$UpdateAction.actionPerformed(MainWindow.java:294)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

2 回答 2

1

在您的“if”语句中,您正在检查jsonObj .get() 的类型,但您正在将tripList .get() 的值分配给您的数组。

我想你的意思是:

JSONArray array2 = (JSONArray) jsonObj.get("Leg");
于 2013-04-04T11:20:17.320 回答
0

TripList不包含Leg.

在这一行:

JSONArray array2 = (JSONArray) tripList.get("Leg");

您尝试从没有的“TripList”中获取“Leg

尝试使用:

JSONArray array2 = (JSONArray) jsonObj.get("Leg");
于 2013-04-04T11:31:01.140 回答