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

このような2番目:

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 の「脚」が配列の場合、配列にループしてデータを取得することです。しかし、「脚」がオブジェクトの場合、ループする必要はありません。掘り下げてデータを取得するだけです。これは私のために働いています

しかし、最初のものは機能せず、nullpointerexpection を返します。

私のコードは次のとおりです。

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が空であるためnullpointerを取得します..だから、nullpointerを与えているのは私の2番目の配列です: 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

0

TripList含まれていませんLeg

この行で:

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

"Leg" を持たない"TripList" から "Leg" を取得しようとしました。

使用してみてください:

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