JSR 353解決了用于JSON處理的標(biāo)準(zhǔn)Java API,并作為JEE 7的一部分提供。
參考實(shí)現(xiàn)jsonp可以從https://jsonp.java.net/download.html下載。
要使用JSON Processing API編譯應(yīng)用程序,請?jiān)趍aven項(xiàng)目中聲明對json-api的依賴關(guān)系。
api依賴不允許您運(yùn)行應(yīng)用程序。
<dependency> <groupId>javax.json</groupId> <artifactId>javax.json-api</artifactId> <version>1.0</version> </dependency>
對于非Maven項(xiàng)目,JSON處理API jar(http://search.maven.org/remotecontent?filepath=javax/json/javax.json-api/1.0/javax.json-api-1.0.jar)可以是下載并包含在用于編譯應(yīng)用程序的類路徑中。
要使用JSON Processing API運(yùn)行應(yīng)用程序,請?jiān)趍aven項(xiàng)目中聲明對json-api實(shí)現(xiàn)的依賴。
<dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.0.4</version> </dependency>
對于非Maven項(xiàng)目,可以通過http://search.maven.org/remotecontent?filepath=org/glassfish/javax.json/1.0.4/ 下載JSON Processing RI jar并將其包含在類路徑中以運(yùn)行應(yīng)用程序javax.json-1.0.4.jar。
GlassFish 4.0已經(jīng)包括JSON實(shí)現(xiàn)。對于Web應(yīng)用程序,我們不需要在戰(zhàn)爭中包含實(shí)現(xiàn)jar。
在開發(fā)應(yīng)用程序期間聲明具有提供的作用域的json-api依賴關(guān)系。
<dependency> <groupId>javax.json</groupId> <artifactId>javax.json-api</artifactId> <scope>provided</scope> <version>1.0</version> </dependency>
以下代碼顯示了如何使用JsonReader將JSON數(shù)據(jù)讀取到JSON對象模型。
import java.io.StringReader; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; import javax.json.JsonValue; public class Main { public static void main(String[] args) { String personJSONData = " {" + " \"name\": \"Jack\", " + " \"age\" : 13, " + " \"isMarried\" : false, " + " \"address\": { " + " \"street\": \"#1234, Main Street\", " + " \"zipCode\": \"123456\" " + " }, " + " \"phoneNumbers\": [\"011-111-1111\", \"11-111-1111\"] " + " }"; JsonReader reader = Json.createReader(new StringReader(personJSONData)); JsonObject personObject = reader.readObject(); reader.close(); System.out.println("Name : " + personObject.getString("name")); System.out.println("Age : " + personObject.getInt("age")); System.out.println("Married: " + personObject.getBoolean("isMarried")); JsonObject addressObject = personObject.getJsonObject("address"); System.out.println("Address: "); System.out.println(addressObject.getString("street")); System.out.println(addressObject.getString("zipCode")); System.out.println("Phone : "); JsonArray phoneNumbersArray = personObject.getJsonArray("phoneNumbers"); for (JsonValue jsonValue : phoneNumbersArray) { System.out.println(jsonValue.toString()); } } }
JsonReader reader = Json.createReader(new StringReader(personJSONData)); JsonArray personArray = reader.readArray(); reader.close(); for (JsonValue jsonVal : personArray) { System.out.println(personObj.getValueType() + " - " + ((JsonObject) personObj).getString("name")); }
以下代碼顯示了如何使用JsonObjectBuilder和JsonArrayBuilder構(gòu)建JSON對象
import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; public class Main { public static void main(String[] args) { JsonObject personObject = Json.createObjectBuilder() .add("name", "John") .add("age", 13) .add("isMarried", false) .add("address", Json.createObjectBuilder().add("street", "Main Street") .add("city", "New York") .add("zipCode", "11111") .build() ) .add("phoneNumber", Json.createArrayBuilder().add("00-000-0000") .add("11-111-1111") .add("11-111-1112") .build() ) .build(); System.out.println("Object: " + personObject); } }
以下代碼顯示了如何使用JsonWriter序列化JSON對象。
import java.io.StringWriter; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; import javax.json.JsonWriter; public class Main { public static void main(String[] args) { JsonObject personObject = Json.createObjectBuilder() .add("name", "Jack") .add("age", 13) .add("isMarried", false) .add("address", Json.createObjectBuilder().add("street", "Main Street") .add("city", "New York") .add("zipCode", "11111") .build() ) .add("phoneNumber", Json.createArrayBuilder().add("00-000-0000") .add("11-111-1111") .add("11-111-1112") .build() ) .build(); StringWriter stringWriter = new StringWriter(); JsonWriter writer = Json.createWriter(stringWriter); writer.writeObject(personObject); writer.close(); System.out.println(stringWriter.getBuffer().toString()); } }
更多建議: