gpt4 book ai didi

java - 哪个 Java/JSON 库支持将包含注释的 JSON 文件转换为 Java 类?

转载 作者:行者123 更新时间:2023-12-02 09:36:10 25 4
gpt4 key购买 nike

我的 JSON 文件的内容是:

{
"MJ" : "Michael Jordan",
"KB" : "Kobe Bryant",
"KG" : "Kevin Garnet"
}

现在,如果我使用 Gson 或 Jackson 或 JsonSimple 或其他 Java/JSON 第三方,则可以轻松将此文件(或字符串)转换为 Java 类(例如:java.util.HashMap)党库。例如,我可以这样使用 Gson:

//this returns the string above
String jsonString = TestReader.getStrFromJSonFile();
Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, String>>() {}.getType();
HashMap<String, String> map = gson.fromJson(jsonString, type);

但是,如果我的 JSON 文件包含注释,大多数第三方库将无法工作。例如:

  {   
//Bulls
"MJ" : "Michael Jordan",
/*Lakers*/
"KB" : "Kobe Bryant",
//Boston Celtics
"KG" : "Kevin Garnet"
}

现在,在谷歌搜索和 stackoverflow 了很多次之后,我什至感到困惑。我有两个问题:

  1. JSON 文件真的可以包含任何注释吗?我认为可以,因为我每天都会在 JSON 文件中看到很多这样的内容。但是>>>(links)

  2. 如果可以,我该如何解决我的问题?

最佳答案

如果您在使用的解析器上启用该功能,Jackson 支持注释:

https://fasterxml.github.io/jackson-core/javadoc/2.10/com/fasterxml/jackson/core/JsonParser.Feature.html

关于java - 哪个 Java/JSON 库支持将包含注释的 JSON 文件转换为 Java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com