1

I have a JSON Object that looks like this :

"TripList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestTrip.xsd",
  "servertime":"11:27",
  "serverdate":"2013-04-02",
  "Trip":[{
    "Leg":{
      "name":"Spårvagn 3",
      "type":"TRAM",
      "id":"9015014500300079",
      "direction":"Kålltorp",
      "fgColor":"#004b85",
      "bgColor":"#ffffff",
      "stroke":"Solid",
      "accessibility":"wheelChair",
      "Origin":{
        "name":"Brunnsparken, Göteborg",
        "type":"ST",
        "id":"9022014001760004",
        "routeIdx":"19",
        "time":"11:27",
        "date":"2013-04-02",
        "track":"D ",
        "rtTime":"11:31",
        "rtDate":"2013-04-02",
        "$":"\n"
        }

to get the name inside the Leg object is working fine. But if I wanna get thetime inside the Origin object how do I do that?

My code is like this so far:

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

    while (iterator.hasNext()) {

        JSONObject jsonObj = iterator.next();
        jsonObj = (JSONObject) jsonObj.get("Leg");
        String line = (String) jsonObj.get("name");
        Planner.getPlanner().setLines(line);
        System.out.println(jsonObj.get("Origin"));
        Long time = (Long) jsonObj.get("time");
        String track =(String) jsonObj.get("track");

        System.out.println(line);
        System.out.println(time);
        System.out.println(track);

    }

}

And in the console it say like this : 

{"routeIdx":"19","id":"9022014001760004","rtDate":"20130402","time":"15:02","$":"\n","name:"Brunnsparken, Göteborg","track":"D ","rtTime":"15:06","date":"2013-04-02","type":"ST"}

Spårvagn 3

null

null

So basiclly i am getting the name Spårvang 3 already. But I wanna get the time.

so the time that I am trying to get out by using jsonObj.get("time"); is giving a null value.

Whats the problem and how can I get the time from the object "Origin"??

1
  • "time" is inside "Origin" and not "Leg". Also, time is not Long. You need to use SimpleDateFormat to parse it. perhaps you'd like to use "date" to parse it. Commented Apr 2, 2013 at 13:15

3 Answers 3

2

Since "Time" is part of the "Origin"-object, you would need to extract the "Origin"-object first:

JSONOject origin = (JSONObject) jsonObj.get("Origin");

And then:

String time = origin.getString("time");
Sign up to request clarification or add additional context in comments.

Comments

1

You are getting 'time' and 'track' properties of 'Leg' object, not the 'Origin' object.. It should be:

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

    while (iterator.hasNext()) {

        JSONObject jsonObj = iterator.next();
        jsonObj = (JSONObject) jsonObj.get("Leg");
        String line = (String) jsonObj.get("name");
        Planner.getPlanner().setLines(line);
        System.out.println(jsonObj.get("Origin"));

        // Added this line
        jsonObj = (JSONObject) jsonObj.get("Origin");

        String time = (String) jsonObj.get("time");
        String track =(String) jsonObj.get("track");

        System.out.println(line);
        System.out.println(time);
        System.out.println(track);

    }

Comments

0

Do following code

JSONOject origin = (JSONObject) jsonObj.get("Origin");
String time = (String) origin.getString("time");

them use SimpleDateFormat to parse time string and get date object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.