找到關於 JSON 的211 篇文章

JSON 和 XML 的區別

Mahesh Parahar
更新於 2020年2月24日 08:08:04

296 次瀏覽

JSON 和 XML 都是程式設計領域最流行的資料傳輸資源。由於它們各自重要的特性和功能,這兩種資源在全球範圍內被廣泛使用。根據它們的特性,以下是 JSON 和 XML 的重要區別:序號|要點|JSON|XML|---|----|----|----|1|縮寫|JSON 代表 JavaScript 物件表示法。而 XML 代表可擴充套件標記語言。|2|型別|JSON 格式是資料可交換的。而 XML 格式是標記語言。|3|基於|JSON 源自 JavaScript 語言,它具有以物件表示方式表示資料的功能。而 XML 源自 SGML 並使用標籤結構... 閱讀更多

正確捕獲 JSON.parse 中的異常

Ayush Gupta
更新於 2019年12月2日 06:17:05

4K+ 次瀏覽

捕獲無效 JSON 解析錯誤的最佳方法是將對 JSON.parse() 的呼叫放入 try/catch 塊中。示例函式 parseJSONSafely(str) {    try {       return JSON.parse(str);    }    catch (e) {       console.err(e);       // 根據用例返回預設物件或 null。       return {}    }}

如何向伺服器傳送和接收 JSON 資料

Ayush Gupta
更新於 2019年11月27日 10:20:22

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 ... 閱讀更多

在 Java 中使用 Gson 如何使用 @SerializedName 註解?

raja
更新於 2020年7月9日 08:17:53

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 ... 閱讀更多

如何在 Java 中使用 Gson 實現自定義 JsonAdapter?

raja
更新於 2020年7月9日 08:15:08

2K+ 次瀏覽

可以使用 @JsonAdapter 註解在欄位或類級別指定 Gson。TypeAdapter 類可用於將 Java 物件轉換為 JSON 和從 JSON 轉換。預設情況下,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 { ... 閱讀更多

如何在 Java 中使用 Jackson API 將 JsonNode 轉換為 ArrayNode?

raja
更新於 2020年7月9日 07:59:35

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\", ... 閱讀更多

如何在 Java 中使用 Jackson 搜尋 JSON 檔案中的值?

raja
更新於 2020年7月9日 07:52:12

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")) {       ... 閱讀更多

何時在 Java 中使用 Jackson 的 @ConstructorProperties 註解?

raja
更新於 2020年7月9日 07:19:56

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);   ... 閱讀更多

如何在 Java 中使用 Gson 向 JSON 字串新增/插入附加屬性?

raja
更新於 2020年2月19日 10:28:54

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); ... 閱讀更多

在 Java 中使用 @JsonUnwrapped 註解的重要性?

raja
更新於 2020年7月9日 06:43:52

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 ... 閱讀更多

廣告
© . All rights reserved.