是否有一个 Java JSON 反序列化器可以将字符串解码为列表字典或原始类型字典列表

发布于 2024-10-07 05:42:58 字数 178 浏览 5 评论 0原文

我需要从数据库中提取不基于标准对象的 JSON 文档。

有没有办法使用JAVA将这些文档“反序列化”为列表和列表?原始对象的字典(字符串、整数、布尔值等)

有什么库可以在两个方向上执行此操作? 换句话说,我正在寻找 System.Runtime.Serialization.Json 的 Java 计数器部分

I need to pull back from a database JSON documents that are not based on a standard object.

Is there a way using JAVA to "deserialize" these documents into Lists & Dictionaries of primitive objects (string, int, bool, etc...)

Any library that can do this in both directions?
In other words I am looking for the Java counter part of System.Runtime.Serialization.Json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-10-14 05:42:58

我建议不要尝试将 JSON 强制转换为难以使用的 MapList,而是可以将 JSON 解析为树的库模型。虽然许多库都可以执行此类操作,但 Google Gson 是我的最爱。您可以使用其 JsonParser 类将 JSON 解析为从根开始的树 JsonElement。一旦您拥有了 JsonElement,它应该比某些可能是 List 和可能是 Map 的 Object 更容易使用

JsonElement e = new JsonParser().parse(someJson);
if (e.isJsonObject()) {
  // JsonObject has many of the same methods as a Map
  JsonObject obj = e.getAsJsonObject();
  String foo = obj.get("foo").getAsString();
  int bar = obj.get("bar").getAsInt();
} else if (e.isJsonArray()) {
  JsonArray array = e.getAsJsonArray();
  // JsonArray implements Iterable and can be used a lot like a List
  for (JsonElement element : array) {
    ...
  }
}

使用这些对象构建 JSON 树也很容易。

Rather than trying to force JSON into a difficult to use Map<String, Object> or List<Object>, I'd recommend a library that can parse JSON into a tree model. While many libraries can do this sort of thing, Google Gson is my favorite. You can use its JsonParser class to parse JSON as a tree starting with a root JsonElement. Once you have the JsonElement, it should be considerably easier to use than some Object that might be a List and might be a Map.

JsonElement e = new JsonParser().parse(someJson);
if (e.isJsonObject()) {
  // JsonObject has many of the same methods as a Map
  JsonObject obj = e.getAsJsonObject();
  String foo = obj.get("foo").getAsString();
  int bar = obj.get("bar").getAsInt();
} else if (e.isJsonArray()) {
  JsonArray array = e.getAsJsonArray();
  // JsonArray implements Iterable and can be used a lot like a List
  for (JsonElement element : array) {
    ...
  }
}

It's easy to build a JSON tree using these objects as well.

偏爱你一生 2024-10-14 05:42:58

几乎 http://json.org/ 上的每个 Java 库都可以做到这一点,您实际上尝试过找到一些东西吗?

例如,对于 Jackson,您会这样做:

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

但创建自己的要映射到的 Java 对象通常很方便;无需使用标准对象。例如:

  public class MyType {
    public String name;
    public int age;
    // and so on
    public Date createTime;
  }

  MyType instance = mapper.readValue(json, MyType.class);

有关更多示例,您可以查看Jackson 教程

Almost every single Java library at http://json.org/ can do this, did you actually try finding something?

For example, with Jackson you would do:

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

but it is often convenient to create your own Java objects to map to; there is no need to use standard objects. For example:

  public class MyType {
    public String name;
    public int age;
    // and so on
    public Date createTime;
  }

  MyType instance = mapper.readValue(json, MyType.class);

For more examples, you can check out Jackson tutorial.

递刀给你 2024-10-14 05:42:58

Flexjson 绝对可以做到。下面的行将 jsonString 反序列化为 HashMap

Map<String, String> map = new flexjson.JSONDeserializer().deserialize(jsonString);

Flexjson definitely does it. The below line de-serialize a jsonString to a HashMap

Map<String, String> map = new flexjson.JSONDeserializer().deserialize(jsonString);
淡淡的优雅 2024-10-14 05:42:58

抱歉,JSON 没有任何有关数据类型的元信息,因此除了字符串列表之外,没有机会将任何不基于标准对象的内容转换回来。

更新:我可以考虑使用 Groovy 来使用未分类的 JSON 字符串制作 POGO。然后您还可以使用动态对象。也许您还可以利用 http://json-lib.sourceforge.net/

Sorry but JSON does not have any metainformation about datatypes, thus there is no chance to convert anything back not based on standard objects, except a list of strings.

UPDATE: I could think of making POGOs with Groovy out of a not classified JSON-String. Then you can also work with dynamic Objects. Maybe you can also utilize http://json-lib.sourceforge.net/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文