JAVAEE8發佈後,現在你可以使用新的JAVAEE API JSON-B(JSR367)
Maven的依賴性:
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
下面是一些代碼快照:
Jsonb jsonb = JsonbBuilder.create();
// Two important API : toJson fromJson
String result = jsonb.toJson(listaDePontos);
JSON-P也更新到1.1,更易於使用。 JSON-P 1.1(JSR374)
Maven的依賴性:
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
下面是可運行的代碼快照:
String data = "{\"name\":\"Json\","
+ "\"age\": 29,"
+ " \"phoneNumber\": [10000,12000],"
+ "\"address\": \"test\"}";
JsonObject original = Json.createReader(new StringReader(data)).readObject();
/**getValue*/
JsonPointer pAge = Json.createPointer("/age");
JsonValue v = pAge.getValue(original);
System.out.println("age is " + v.toString());
JsonPointer pPhone = Json.createPointer("/phoneNumber/1");
System.out.println("phoneNumber 2 is " + pPhone.getValue(original).toString());
又有什麼問題呢? – Blackbelt
我很難序列化的對象,因爲我是一個小noobie –
谷歌有關傑克遜lib。它會幫助你。 – user