无法读取整个 JSON 文件

Can't read a whole JSON file

正如标题所说,我无法将整个 JSON 文件反序列化为 ArrayList,更具体地说,我的代码只读取文件中的第一项而忽略其余项。

您的 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 个对象的列表,而不是一次提供一个对象(使用此解决方案编写时无需追加):

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

这意味着您的方法必须接受 MyClass 的列表,而不是单个 MyClass:

private static void writeJSON(List<MyClass> objectsToSerialize) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myclass.json")));
    mapper.writeValue(out, objectsToSerialize);
}

如果您需要不断向 JSON 文件中添加内容,您可以执行以下操作:

public class Main {
    public static void main(String[]args) throws IOException {
        // Read the file
        List<MyClass> myClasses = readFromFile();
        
        // Add whatever MyClass objects you want to the read List
        myClasses.add(new MyClass("Name1", "Country1", new double[] { 1.0 }));
        myClasses.add(new MyClass("Name2", "Country2", new double[] { 2.0 }));
        myClasses.add(new MyClass("Name3", "Country3", new double[] { 3.0 }));
        
        // Write the whole List again
        writeJSON(myClasses);
    }

    private static List<MyClass> readFromFile() throws IOException {
        String jsonString = FileUtils.readFileToString(new File("myclass.json"), StandardCharsets.UTF_8);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(jsonString, new TypeReference<List<MyClass>>() {});
    }

    private static void writeJSON(List<MyClass> objectsToSerialize) throws IOException {
        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 格式化程序以验证它是否有效。这是一个潜在的link:https://jsonlint.com/.