九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
JSONSerializer.writeWithFieldName
 如何:對 JSON 數(shù)據(jù)進行序列化和反序列化
This content was manually translated to a higher quality standard. If you wish to further improve the quality of the translation, click the Edit button associated with the sentence that you wish to modify.
How to: Serialize and Deserialize JSON Data

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and AJAX-enabled Web services.

This topic demonstrates how to serialize .NET type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of .NET types using theDataContractJsonSerializer. This example uses a data contract to demonstrate serialization and deserialization of a user-definedPersontype.

Normally, JSON serialization and deserialization is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints. However, in some cases you may need to work with JSON data directly - this is the scenario that this topic demonstrates.

Note

If an error occurs during serialization of an outgoing reply on the server or the reply operation throws an exception for some other reason, it may not get returned to the client as a fault.

This topic is based on the JSON Serialization sample.

To define the data contract for a Person

  • Define the data contract forPersonby attaching the DataContractAttribute to the class andDataMemberAttribute attribute to the members you want to serialize. For more information about data contracts, seeDesigning Service Contracts.

    [DataContract]
        internal class Person
        {
            [DataMember]
            internal string name;
    
            [DataMember]
            internal int age;
        }

To serialize an instance of type Person to JSON

  1. Create an instance of thePersontype.

    Person p = new Person();
    p.name = "John";
    p.age = 42;
  2. Serialize thePersonobject to a memory stream using theDataContractJsonSerializer.

    MemoryStream stream1 = new MemoryStream();
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
  3. Use the WriteObject method to write JSON data to the stream.

    ser.WriteObject(stream1, p);
  4. Show the JSON output.

    stream1.Position = 0;
    StreamReader sr = new StreamReader(stream1);
    Console.Write("JSON form of Person object: ");
    Console.WriteLine(sr.ReadToEnd());

To deserialize an instance of type Person from JSON

  1. Deserialize the JSON-encoded data into a new instance ofPersonby using the ReadObject method of theDataContractJsonSerializer.

    stream1.Position = 0;
    Person p2 = (Person)ser.ReadObject(stream1);
  2. Show the results.

    Console.Write("Deserialized back, got name=");
    Console.Write(p2.name);
    Console.Write(", age=");
    Console.WriteLine(p2.age);
The JSON serializer throws a serialization exception for data contracts that have multiple members with the same name, as shown in the following sample code.
如何:對 JSON 數(shù)據(jù)進行序列化和反序列化

JSON(JavaScript 對象符號)是一種高效的數(shù)據(jù)編碼格式,可用于在客戶端瀏覽器和支持 AJAX 的 Web 服務(wù)之間快速交換少量數(shù)據(jù)。

本主題演示如何使用 DataContractJsonSerializer 將 .NET 類型對象序列化為 JSON 編碼數(shù)據(jù),然后將 JSON 格式的數(shù)據(jù)反序列化回 .NET 類型的實例。本示例使用數(shù)據(jù)協(xié)定來演示用戶定義的Person類型的序列化和反序列化。

通常,當在通過支持 AJAX 的終結(jié)點公開的服務(wù)操作中使用數(shù)據(jù)協(xié)定類型時,Windows Communication Foundation (WCF) 會自動處理 JSON 序列化和反序列化。但是,在某些情況下您可能需要直接處理 JSON 數(shù)據(jù),這正是本主題演示的方案。

說明

如果在服務(wù)器上序列化傳出答復(fù)期間出現(xiàn)錯誤,或者答復(fù)操作由于某個其他原因引發(fā)異常,則可能不會將其作為錯誤返回到客戶端。

本主題基于 JSON 序列化示例。

定義 Person 的數(shù)據(jù)協(xié)定

  • 通過將 DataContractAttribute 附加到類并將DataMemberAttribute 特性附加到要序列化的成員,為Person定義數(shù)據(jù)協(xié)定。有關(guān)以下內(nèi)容的詳細信息數(shù)據(jù)協(xié)定的更多信息,請參見設(shè)計服務(wù)協(xié)定

    [DataContract]
        internal class Person
        {
            [DataMember]
            internal string name;
    
            [DataMember]
            internal int age;
        }

將 Person 類型的實例序列化為 JSON

  1. 創(chuàng)建Person類型的實例。

    Person p = new Person();
    p.name = "John";
    p.age = 42;
  2. 使用 DataContractJsonSerializer 將Person對象序列化為內(nèi)存流。

    MemoryStream stream1 = new MemoryStream();
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
  3. 使用 WriteObject 方法將 JSON 數(shù)據(jù)寫入到流中。

    ser.WriteObject(stream1, p);
  4. 顯示 JSON 輸出。

    stream1.Position = 0;
    StreamReader sr = new StreamReader(stream1);
    Console.Write("JSON form of Person object: ");
    Console.WriteLine(sr.ReadToEnd());

從 JSON 反序列化 Person 類型的實例

  1. 通過使用 DataContractJsonSerializer 的 ReadObject 方法,將 JSON 編碼數(shù)據(jù)反序列化為一個新的Person實例。

    stream1.Position = 0;
    Person p2 = (Person)ser.ReadObject(stream1);
  2. 顯示結(jié)果。

    Console.Write("Deserialized back, got name=");
    Console.Write(p2.name);
    Console.Write(", age=");
    Console.WriteLine(p2.age);
對于包含多個具有相同名稱的成員的數(shù)據(jù)協(xié)定,JSON 序列化程序?qū)⒁l(fā)一個序列化異常,如以下示例代碼中所示。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#中,Json的序列化和反序列化的幾種方式總結(jié)
[.net 面向?qū)ο蟪绦蛟O(shè)計進階] (13) 序列化(Serialization)(五) JSON序列化利器 Newtonsoft.Json 及 通用Json類
json序列化整理
對象序列化
C#序列化與反序列化詳解
ASP.NET中JSON的序列化和反序列化
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服