[英]Can't read a whole JSON file into ArrayList (ArrayList Deserialization)
正如標題所說,我無法將整個 JSON 文件反序列化為 ArrayList,更具體地說,我的代碼僅讀取文件中的第一項並忽略 Z65E8800B5C6800AAD896F888B2A6。
這是我的寫作方法:
private static void writeJSON(MyClass myClassObject) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myclass.json", true)));
mapper.writeValue(out, myClassObject);
}
每當我想序列化 object 時,我都會在我的主代碼中調用此方法,它工作正常。
這是我的閱讀方法:
private static void readFromFile() throws IOException {
String jsonString = FileUtils.readFileToString(new File("myclass.json"), StandardCharsets.UTF_8);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
ArrayList<MyClass> myClassArray = mapper.readValue(jsonString, new TypeReference<ArrayList<MyClass>>() {});
}
在添加這部分之前
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
和 MyClass class 中的默認構造函數,我遇到了一些異常,更具體地說是“無法從 START_OBJECT 令牌中反序列化 java.util.ArrayList 的實例”
但是,現在它可以正常工作,沒有任何錯誤或異常,但是通過打印 myClassArray ArrayList 我意識到它只包含第一個 object (及其值)。 試圖找出問題,我注意到如果我打印jsonString值,我會得到整個 JSON 文件(作為字符串)。
所以我猜這個問題與這部分代碼有關
ArrayList<MyClass> myClassArray = mapper.readValue(jsonString, new TypeReference<ArrayList<MyClass>>() {});
但我無法弄清楚。
MyClass class 看起來像這樣:
public class MyClass {
private String name;
private String country;
private double features[];
private Date timestamp = new Date();
public City() { //The empty constructor is used for the needs of the readFromFile method in Main class.
}
public City(String name, String country) {
super();
this.name = name;
this.country = country;
}
public City(String name,String country,double[] features) {
this.name = name;
this.country = country;
this.features = Arrays.copyOf(features, features.length);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double[] getFeatures() {
return Arrays.copyOf(features, features.length);
}
public void setFeatures(double[] features) {
this.features = features;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
和 JSON 文件現在(忽略零和數字,整個事情是半完成的):
{"name":"John","country":"gr","features":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.7123809523809526,0.75,0.0],"timestamp":1637924593676}{"name":"Scott","country":"gb","features":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6598639455782312,0.2,0.15601209271073035],"timestamp":1637924610010}
{"name":"Michael","country":"it","features":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6877551020408165,0.75,0.06856164464370273],"timestamp":1638458784201}
您的 json 文件不包含有效的 json 數組。 它應該看起來像:
[
{ ... },
{ ... }
]
不是
{ ... }
{ ... }
我認為您將 json 寫為單個對象,而不是數組。
您的 JSON 文件無效。 您只有幾個 JSON 對象,但您需要將它們捆綁在一個 JSON 數組中,如下所示:
[
{
"name": "John",
"country": "gr",
"features": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.7123809523809526,
0.75,
0.0
],
"timestamp": 1637924593676
},
{
"name": "Scott",
"country": "gb",
"features": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.6598639455782312,
0.2,
0.15601209271073035
],
"timestamp": 1637924610010
},
{
"name": "Michael",
"country": "it",
"features": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.6877551020408165,
0.75,
0.06856164464370273
],
"timestamp": 1638458784201
}
]
為了修正你的寫作方法,你需要為它提供一個MyClass
對象的列表,而不是一次一個(你不需要 append 在使用這個解決方案寫作時):
public class Main {
public static void main(String[]args) throws IOException {
List<MyClass> objectsToSerialize = new ArrayList<>();
objectsToSerialize.add(new MyClass("Name1", "Country1", new double[] { 1.0 }));
objectsToSerialize.add(new MyClass("Name2", "Country2", new double[] { 2.0 }));
objectsToSerialize.add(new MyClass("Name3", "Country3", new double[] { 3.0 }));
ObjectMapper mapper = new ObjectMapper();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myclass.json")));
mapper.writeValue(out, objectsToSerialize);
}
}
我假設您使用的映射器是 Jackson 的Object Mapper
。 在您的代碼行中:
ArrayList<MyClass> myClassArray = mapper.readValue(jsonString, new TypeReference<ArrayList<MyClass>>() {});
沒有真正的錯誤。 相反,您糾正先前錯誤的方法是有缺陷的:
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
您只是繞過了最初的錯誤,問題是您的 JSON 格式不正確,因此無法將其轉換為集合。 檢查您的原始文件,或將字符串放入在線 JSON 格式化程序以驗證它是否有效。 這是一個潛在的鏈接: https://jsonlint.com/ 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.