我有一个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());
}
}
感谢这对我工作!但数据有很多Long内容,现在没有全部显示,我尝试将for循环中的int更改为LOng,但没有解决问题 – firroaga
太棒了!我很高兴这对你有所帮助。 “长内容”是什么意思 - 你的意思是你的JSON文件中有很多项目?像多少物品? – ishmaelMakitla