java 将 JSON 转换为 POJO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6426642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert JSON to POJO
提问by Marouane Gazanayi
I am using Jersey for REST WS, and I get my response as JSON.
我正在将 Jersey 用于 REST WS,并且我的响应为 JSON。
I want to transform this response to a POJO. How to do it ?
我想将此响应转换为 POJO。怎么做 ?
回答by Programmer Bruce
To convert between Java and JSON, there are a good number of APIs available to choose from.
为了在 Java 和 JSON 之间进行转换,有很多 API 可供选择。
You could "manually" step through the JSON components and extract values to populate Java objects, or you could use a JSON-to-Java binding API to take care of many low-level mapping concerns.
您可以“手动”逐步执行 JSON 组件并提取值以填充 Java 对象,或者您可以使用 JSON-to-Java 绑定 API 来处理许多低级映射问题。
Hymansonis such an API. It's easy to use and provides probably the fullest set of API features to address common issues and customizations. StackOverflow.com has many examples of how to use it.
Hymanson就是这样一个 API。它易于使用并且可能提供最完整的 API 功能集来解决常见问题和自定义。StackOverflow.com 有很多关于如何使用它的例子。
回答by weekens
As an option, you can check JSON Simple.
作为一个选项,您可以检查JSON Simple。
回答by Mario Dennis
You can also use JaxB binding which would handle conversion to and from for you. Its part of Java SE so no jar downloads required.However, you would need to write a pojo class with all the attributes your json object will return and accessor methods to assess them. Then you would add a XmlRootElement annotation on that class this will enable jaxb to convert to and from json where necessary. Example:
您还可以使用 JaxB 绑定来处理与您之间的转换。它是 Java SE 的一部分,因此不需要下载 jar。但是,您需要编写一个 pojo 类,其中包含 json 对象将返回的所有属性和访问器方法来评估它们。然后,您将在该类上添加 XmlRootElement 注释,这将使 jaxb 能够在必要时与 json 相互转换。例子:
POJO
POJO
@XmlRootElement
public class User
{
private String name;
public void setName (String name)
{
this.name = name;
}
public String getName ()
{
return name;
}
}
}
Jersey service
泽西岛服务
@GET
@Produces (MediaType.APPLICATION_JSON)
public User getUser ()
{
User user = new User ();
user.setName ("John Doe");
return user;
}
This would convert the User POJO object and return it has the media type specified in this example JSON. You can even return it with a Response object. Example:
这将转换 User POJO 对象并返回它具有此示例 JSON 中指定的媒体类型。您甚至可以使用 Response 对象返回它。例子:
@GET
@Produces (MediaType.APPLICATION_JSON)
public Response getUser ()
{
User user = new User ();
user.setName ("John Doe");
return Response.status (Status.OK).entity (user).build ();
}
This returns a Response object with code 200 (Success) along with User JSON object. [NOTE] This approach is preferred cause It provide the user who invokes your web service information about the status of the transaction or service
这将返回一个带有代码 200(成功)的响应对象以及用户 JSON 对象。[注意] 这种方式是首选原因,它为调用您的 Web 服务的用户提供有关交易或服务状态的信息
回答by akshaymani
We can also make use of below given dependency and plugin in your pom file.
我们还可以在您的 pom 文件中使用以下给定的依赖项和插件。
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.7.1</version>
</dependency>
<plugin>
<groupId>com.googlecode.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.3.7</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
With the use of these you can generate POJO's as per your JSON Schema and then make use of code given below to populate request JSON object via src object specified as parameter to gson.toJson(Object src)
:
通过使用这些,您可以根据您的 JSON 模式生成 POJO,然后使用下面给出的代码通过指定为参数的 src 对象填充请求 JSON 对象gson.toJson(Object src)
:
Gson gson = new GsonBuilder().create();
String payloadStr = gson.toJson(data.getMerchant().getStakeholder_list());
回答by homac
Was looking for the same and since others require you to add annotations or write/generate getter and setter methods for your variables, I wrote my own codec to deal with JSON<->POJO transformations using reflections.
正在寻找相同的内容,并且由于其他人要求您为变量添加注释或编写/生成 getter 和 setter 方法,我编写了自己的编解码器来使用反射处理 JSON<->POJO 转换。
See here: http://homac.cakelab.org/projects/org.cakelab.json