【问题标题】:Can't read a whole JSON file无法读取整个 JSON 文件
【发布时间】:2022-01-09 03:47:35
【问题描述】:

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

【问题讨论】:

  • 我们需要 MyClass 类代码和您尝试反序列化的 JSON 才能为您提供帮助。
  • mapper.readValue() 实际上做了什么?听起来它读取单个值并在 ArrayList 中返回它。您可能需要一个循环来读取所有值并将每个值添加到您的数组列表中

标签: java json jackson


【解决方案1】:

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

【讨论】:

  • 好吧,我想问题出在我的写作方法上。我应该在那里解决什么问题?
  • 让我检查一下;)
  • 我已经更新了我的答案。请检查它;)
  • 谢谢你,我已经更新了我的初始帖子,请检查它!
  • 正如我所写的,您需要一次序列化列表,而不是一次序列化一个 MyClass。按照我的回答建议去做。
【解决方案2】:

您的 json 文件不包含有效的 json 数组。 它应该看起来像:

[ 
 { ... },
 { ... }
]

没有

{ ... }
{ ... }

我认为您将 json 编写为单个对象,而不是数组。

【讨论】:

    【解决方案3】:

    我假设您使用的映射器是 Jackson 的 Object Mapper。在您的代码行中:

    ArrayList<MyClass> myClassArray = mapper.readValue(jsonString, new TypeReference<ArrayList<MyClass>>() {});
    

    没有真正的错误。相反,您纠正先前错误的方法是有缺陷的:

    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    

    您只是绕过了最初的错误,问题是您的 JSON 格式不正确,因此无法将其转换为 Collection。检查您的原始文件,或将字符串放入在线 JSON 格式化程序以验证它是否有效。这是一个潜在的链接:https://jsonlint.com/

    【讨论】:

      猜你喜欢
      • 2015-08-19
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      相关资源
      最近更新 更多