[英]Is there a java 11 native way to convert json to an object?
This question: How to parse JSON in Java Has several answers which require including 3rd party libraries, including GSON, org.json and Jackson.
I am using Adobe AEM, which is notoriously intolerant of including 3rd party libraries, so am trying to do the REST API calling with standard Java 11. java 11 has the HTTPClient classes, which will give me JSON from the response.
現在我需要將 Json 轉換為所需的 java object。
例如,我有一個帶有姓名和年齡字段的人 class,並希望從類似 {"name":"bob", "age":12}
我還需要一些東西來將 Java object 轉換為 Json。
有什么建議么?
在 java 7 中有一個叫做 JsonObject 的東西,但我找不到任何顯示這種轉換的例子。
您可以使用 JavaEE 8 中引入的JsonB
Jsonb jsonb = JsonbBuilder.create();
Person person = jsonb.fromJson(personString, Person.class);
如果您使用maven
,這里是要添加的依賴項
<!-- JSON-B API -->
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
</dependency>
<!-- JSON-B RI -->
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.