2016-05-30 89 views
1

我有一个Json数组文件,它有多个数据。 正如你在下面的数据中看到的那样,你会多次注意到timeLastUpdate。 当我用Java编译时,我得到的输出只是JSON中的第一次上传更新。我想要在Java中显示所有timeLastUpdate。 我该怎么做?数据如何使用Java在Json中循环?

例如:

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

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


     } 

    } 

回答

0

对于使用ObjectMapper解析JSON数组,您可以查看所选答案here。 基本上,你的代码应该是这个样子:

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

给它一个尝试,让我们知道,如果这可以帮助你。

+0

感谢这对我工作!但数据有很多Long内容,现在没有全部显示,我尝试将for循环中的int更改为LOng,但没有解决问题 – firroaga

+0

太棒了!我很高兴这对你有所帮助。 “长内容”是什么意思 - 你的意思是你的JSON文件中有很多项目?像多少物品? – ishmaelMakitla

0

当你要去杰克逊,你可以这样写的船友列表:

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

即使你可以使用以下代码行将其作为列表读取:

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

,你可以使用一个简单的foreach来读取数组或列表如下目标:

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

你应该只读取数据作为一个数组,这之后,你可以通过它进行迭代:

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

}