找到 211 篇文章 關於 JSON

296 次瀏覽
JSON 和 XML 都是程式設計世界中最流行的資料傳輸資源。由於它們各自的各種重要特性和功能,這兩種資源在全球範圍內得到廣泛使用。根據其功能,以下是 JSON 和 XML 的重要區別序號 關鍵 JSON XML 1 縮寫 JSON 代表 JavaScript 物件表示法。另一方面,XML 代表可擴充套件標記語言。 2 型別 JSON 格式是資料可交換的。另一方面,XML 格式是標記語言。 3 基於 JSON 來源於 JavaScript 語言,它從那裡獲得了表示資料的方式,即以表示物件的方式。另一方面,XML 來源於 SGML,並使用標籤結構 ... 閱讀更多

3K+ 次瀏覽
JavaScript 可以向伺服器傳送網路請求並載入 JSON。JS 使用稱為 AJAX 的東西來做到這一點。AJAX 代表非同步 JavaScript 和 XML。JS 有一個 API,fetch,用於向伺服器獲取(接收)和釋出(傳送)資訊。您可以使用 fetch 以以下方式獲取 JSON 資料 -示例const URL = 'https://jsonplaceholder.typicode.com/todos/1' // 向伺服器傳送不帶任何資料的 GET 請求 fetch(URL, {method: "GET"}) // 從原始響應中獲取 JSON 資料 .then(res => res.json()) // 列印結果 .then(console.log)輸出這將給出以下輸出 -{ "userId": 1, "id": 1, "title": "delectus ... 閱讀更多

7K+ 次瀏覽
@SerializedName 註解可用於使用不同的名稱序列化欄位,而不是實際的欄位名稱。我們可以將預期的序列化名稱作為註解屬性提供,Gson 可以確保使用提供的名稱讀取或寫入欄位。語法@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedName示例import com.google.gson.*; import com.google.gson.annotations.*; public class SerializedNameTest { public static void main(String args[]) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); Person person = new Person(115, "Raja Ramesh", "Hyderabad"); String jsonStr = gson.toJson(person); System.out.println(jsonStr); } } // Person 類 class ... 閱讀更多

2K+ 次瀏覽
@JsonAdapter 註解可以在欄位或類級別使用以指定 Gson。TypeAdapter 類可用於將 Java 物件轉換為 JSON 和從 JSON 轉換為 Java 物件。預設情況下,Gson 庫透過使用內建型別介面卡將應用程式類轉換為 JSON,但我們可以透過提供自定義型別介面卡來覆蓋它。語法@Retention(value=RUNTIME) @Target(value={TYPE, FIELD}) public @interface JsonAdapter示例import java.io.IOException; import com.google.gson.Gson; import com.google.gson.TypeAdapter; import com.google.gson.annotations.JsonAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public class JsonAdapterTest { public static void main(String[] args) { Gson gson = new Gson(); System.out.println(gson.toJson(new Customer())); } } // Customer 類 class Customer { ... 閱讀更多

15K+ 次瀏覽
JsonNode 是所有形成 JSON 樹模型的 JSON 節點的基類,而 ArrayNode 是表示從 JSON 內容對映的陣列的節點類。我們可以透過將 ArrayNode 型別轉換為使用 ObjectMapper 類的 readTree() 方法檢索值並將 get() 方法用於訪問陣列節點指定元素的值來將 JsonNode 轉換為 ArrayNode。語法public JsonNode readTree(String content) throws IOException, com.fasterxml.jackson.core.JsonProcessingException示例import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.core.JsonProcessingException; public class JSonNodeToArrayNodeTest { public static void main(String args[]) throws JsonProcessingException { String jsonStr = "{\"Technologies\" : [\"Java\", ... 閱讀更多

3K+ 次瀏覽
com.fasterxml.jackson.databind.node.ObjectNode 類可用於對映 Json 內容中的 JSON 物件結構。我們可以使用 ObjectNode 類的 get() 方法在 JSON 檔案中搜索特定值,此方法用於訪問物件節點指定欄位的值。語法public JsonNode get(String fieldName)示例import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; public class ObjectNodeTest { public static void main(String args[]) throws Exception { String jsonString = "{\"Id\":101, \"name\":\"Raja Ramesh\", \"address\":\"Madhapur\"}"; ObjectMapper mapper = new ObjectMapper(); ObjectNode node = mapper.readValue(jsonString, ObjectNode.class); if(node.has("name")) { ... 閱讀更多

2K+ 次瀏覽
@ConstructorProperties 註解來自 java.beans 包,用於透過帶註解的建構函式將 JSON 反序列化為 Java 物件。此註解從 Jackson 2.7 版本開始支援。此註解的工作方式非常簡單,而不是在建構函式中的每個引數上都添加註解,我們可以為每個建構函式引數提供一個包含屬性名稱的陣列。語法@Documented @Target(value=CONSTRUCTOR) @Retention(value=RUNTIME) public @interface ConstructorProperties示例import com.fasterxml.jackson.databind.ObjectMapper; import java.beans.ConstructorProperties; public class ConstructorPropertiesAnnotationTest { public static void main(String args[]) throws Exception { ObjectMapper mapper = new ObjectMapper(); Employee emp = new Employee(115, "Raja"); String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp); ... 閱讀更多

8K+ 次瀏覽
com.google.gson.JSonElement 類表示 Json 的一個元素。我們可以使用 Gson 類的 toJsonTree() 方法將物件的表示形式序列化為 JsonElements 的樹。我們可以透過使用 JSonElement 的 getAsJsonObject() 方法向 JSON 字串新增/插入附加屬性。此方法返回獲取元素作為 JsonObject。語法public JsonObject getAsJsonObject()示例import com.google.gson.*; public class AddPropertyGsonTest { public static void main(String[] args) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); // 美化列印 JSON Student student = new Student("Adithya"); String jsonStr = gson.toJson(student, Student.class); System.out.println("JSON 字串: " + jsonStr); ... 閱讀更多

613 次瀏覽
@JsonUnwrapped 註解可用於在序列化和反序列化過程中解包值。它有助於將組合類的值呈現為屬於父類的一部分。語法@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonUnwrapped示例import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class JsonUnwrappedAnnotationTest { public static void main(String args[]) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee()); System.out.println(jsonString); } } class Employee { public int empId = 110; public String empName = "Raja Ramesh"; @JsonUnwrapped ... 閱讀更多