简体   繁体   English

如何在 Java 应用程序中编组/解组 JSON?

[英]How to marshal/unmarshal JSON in a Java application?

Assuming that I have the following JSON string:假设我有以下 JSON 字符串:

{
  "1": {
    "id": "12",
    "name": "xyz"
  },
  "2": {
    "id": "12",
    "name": "xyz"
  },
  "3": {
    "id": "12",
    "name": "xyz"
  },
  "4": {
    "id": "12",
    "name": "xyz"
  },
  "5": {
    "id": "12",
    "name": "xyz"
  },
  "6": {
    "id": "12",
    "name": "xyz"
  }
}

How would one go about creating this JSON structure from within a Java application?如何从 Java 应用程序中创建这个 JSON 结构? Are there any good libraries to aid in marshaling and unmarshaling JSON for Java?是否有任何好的库可以帮助编组和解组 Java 的 JSON?

There are several popular libraries: jackson already mentioned here, gson, org.json and others.有几个流行的库:这里已经提到的 jackson、gson、org.json 等。
There are also some convenience libraries that not dealing with serialization but can be useful for parsing, for example https://github.com/jayway/JsonPath .还有一些不处理序列化但可用于解析的便利库,例如https://github.com/jayway/JsonPath It possibly (didn't test it by myself) can be a bit slower but more convenient.它可能(没有自己测试过)可能会慢一点但更方便。
If you do not handle of tons of json documents per second or very large/comples documents (or both), you probably can select any library that looks simple for you.如果您不每秒处理大量 json 文档或非常大/复杂的文档(或两者),您可能可以选择任何对您来说看起来简单的库。 If you deal with cases that I mentioned, you'll need to read some performance test results in google or run your own, if possible.如果您处理我提到的情况,则需要在 google 中阅读一些性能测试结果或运行自己的(如果可能)。

With libraries like jackson-core , you can easily transform json into an object representation and vice versa easily and efficiently with minimal code.使用jackson-core 之类的库,您可以使用最少的代码轻松高效地将 json 转换为对象表示,反之亦然。

For example, the JSON you showed would be a Map<String,SomeObject> where SomeObject would be represented as follows:例如,您显示的 JSON 将是Map<String,SomeObject> ,其中SomeObject将表示如下:

public class SomeObject {
  private String id;
  private String name;
}

The map would be populated as follows, assuming a constructor that takes id and name for the map entry's value.地图将按如下方式填充,假设构造函数采用idname作为地图条目的值。

Map<String, SomeObject> map = new LinkedHashMap<>();
map.put( "1", new SomeObject( "12", "xyz" ) );
map.put( "2", new SomeObject( "12", "xyz" ) );
map.put( "3", new SomeObject( "12", "xyz" ) );
map.put( "4", new SomeObject( "12", "xyz" ) );
map.put( "5", new SomeObject( "12", "xyz" ) );
map.put( "6", new SomeObject( "12", "xyz" ) );

Now to get the JSON string from Java:现在从 Java 获取 JSON 字符串:

String output = new ObjectMapper().writeValueAsString( map );

If you had the JSON string and you wanted to get the object representation:如果您有 JSON 字符串并且想要获取对象表示:

ObjectMapper mapper = new ObjectMapper();
Map<String, SomeObject> map = new LinkedHashMap<>();
map = mapper.readValue( json, new TypeReference<Map<String,SomeObject>>(){} );   

Obviously this is a basic overview.显然,这是一个基本的概述。 It'd be wise to read the documentation for the Jackson API hosted on their github here .它会是明智的阅读文档的杰克逊API托管在GitHub上了这里

I would to recommend another library: fastjson .我会推荐另一个库: fastjson it's simple, faster and smaller, comparing Jackson.与杰克逊相比,它更简单、更快、更小。

Map<String, Object> map = JSON.parseObject(json, Map.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM