1

I have a Json array file and it has multiple data. As you can see in the data bellow you'll notice timeLastUpdate multiple times. When I compile in Java, the output i get is only the first timeLastUpdate in the JSON. I want all of the timeLastUpdate shown in Java. How do i do that?

example of data:

 [
      {
        "mmsi": "253336000",
        "status": "RESTRICTED_MANEUVERABILITY",
        "courseOverGround": 7.9,
        "timeLastUpdate": 1464545149000,
        "
      },
      {
        "mmsi": "253336000",    
        "timeLastUpdate": 1464545209000,
        "destination": "ZEEBRUGGE",
          },
]

Code Java :

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import java.io.File;
import java.net.URL;
import java.io.IOException;

public class ShipMain {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

             File jsonFile2 = new File("shiphistory.json");

             ShipData shiphistory  = null; 

             ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally

             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

                shiphistory = mapper.readValue(jsonFile2, ShipData.class);
                System.out.println("______________ History_________");
                System.out.println("Ship Name : " + shiphistory.getName());
                System.out.println("Ship MMSI : " + shiphistory.getMmsi());
                System.out.println("Ship Type : " + shiphistory.getShipType());
                System.out.println("Ship Departure : " + shiphistory.getTimeLastUpdate());


        }

    }
Kara
  • 6,115
  • 16
  • 50
  • 57
firroaga
  • 93
  • 1
  • 1
  • 6

3 Answers3

0

As you are going Jackson you can read a list of Shipmate like this :

ShipData[] data = mapper.readValue(json, ShipData[].class);

Even you can read it as a list using following line of code :

List< ShipData > data = mapper.readValue(jsonInput, new TypeReference<List< ShipData>>(){});

you can use a simple foreach to read objects from array or list as below:

System.out.println("______________ History_________"); 
for(ShipData shipData : data){

    System.out.println("Ship Name : " + shipData.getName());
    System.out.println("Ship MMSI : " + shipData.getMmsi());
    System.out.println("Ship Type : " + shipData.getShipType());
    System.out.println("Ship Departure : " + shipData.getTimeLastUpdate());
}
Sir1
  • 732
  • 1
  • 8
  • 18
0

You should just read data as an array and, after that, you can iterate through it:

public static void main(String[] args) throws IOException {

    File jsonFile2 = new File("shiphistory.json");

    ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    ShipData[] shipHistory = mapper.readValue(jsonFile2, ShipData[].class);

    for (ShipData shipData : shipHistory) {
        System.out.println("______________ History_________");
        System.out.println("Ship Name : " + shipData.getName());
        System.out.println("Ship MMSI : " + shipData.getMmsi());
        System.out.println("Ship Type : " + shipData.getShipType());
        System.out.println("Ship Departure : " + shipData.getTimeLastUpdate());
    }

}
Alex K
  • 56
  • 3
0

For parsing JSON array with ObjectMapper you can look at the selected answer here. Basically, your code should look something like this:

ObjectMapper mapper = new ObjectMapper(); 
ShipData[] shipDataArray  = mapper.readValue(jsonFile2, ShipData[].class);
//then you can look through the array
System.out.println("______________ History_________");
for(int x=0; x < shipDataArray.length(); x++){
     shiphistory = shipDataArray[x];
     System.out.println("Ship Name : " + shiphistory.getName());
     System.out.println("Ship MMSI : " + shiphistory.getMmsi());
     System.out.println("Ship Type : " + shiphistory.getShipType());
     System.out.println("Ship Departure : " + shiphistory.getTimeLastUpdate());
    //just a separating line (not necessary - just to separate the shipdata items
    System.out.println("-----------------------------------------------------");
}

Give it a try and let us know if this helps you.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • Thanks this worked for me! But the data has a lot of Long contents and not showing all of it now, I tried changing the int in the for loop to LOng but that didnt resolve the issue – firroaga May 31 '16 at 08:06
  • Great! I am glad this helped you. What do you mean by "long contents" - you mean there are many items in your JSON file? Like how many items? – ishmaelMakitla May 31 '16 at 08:24