發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導圖
隨筆
相冊
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
在做asp.net和unity進行http通信的時候,當unity客戶端發(fā)出表單請求的時候,我要將他要請求的數(shù)據(jù)以json的格式返回給客戶端,讓客戶端來解析。服務(wù)器端這一塊就涉及到j(luò)son的序列化和反序列化的問題。
接下來就來舉個列子,當然包括兩種方法(本篇參考自:http://www./csharpspace/10822r2908.shtml)
兩種方法都有例子,第一種是用泛型集合來存儲的對象,然后將集合序列化一下;第二種是直接序列化的一個對象
using System; using System.Collections.Generic; using System.Web.Script.Serialization; using System.Configuration; using System.Runtime.Serialization.Json; using System.Runtime.Serialization; using System.IO; using System.Text; namespace WebApplication1 { //方法一:引入System.Web.Script.Serialization命名空間使用 JavaScriptSerializer類實現(xiàn)簡單的序列化 [Serializable] public class Person { private int id; /// <summary> /// id /// </summary> public int Id { get { return id; } set { id = value; } } private string name; /// <summary> /// 姓名 /// </summary> public string Name { get { return name; } set { name = value; } } } //方法二:引入 System.Runtime.Serialization.Json命名空間使用 DataContractJsonSerializer類實現(xiàn)序列化 //可以使用IgnoreDataMember:指定該成員不是數(shù)據(jù)協(xié)定的一部分且沒有進行序列化,DataMember:定義序列化屬性參數(shù),使用DataMember屬性標記字段必須使用DataContract標記類 否則DataMember標記不起作用。 [DataContract] public class Person1 { [IgnoreDataMember] public int Id { get; set; } [DataMember(Name = "name")] public string Name { get; set; } [DataMember(Name = "sex")] public string Sex { get; set; } } public partial class _Default : System.Web.UI.Page { string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { Person p1 = new Person(); p1.Id = 1; p1.Name = "dxw"; Person p2 = new Person(); p2.Id = 2; p2.Name = "wn"; List<Person> listperson = new List<Person>(); listperson.Add(p1); listperson.Add(p2); JavaScriptSerializer js = new JavaScriptSerializer(); //json序列化 string s = js.Serialize(listperson); Response.Write(s); //方法二 Person1 p11 = new Person1(); p11.Id = 1; p11.Name = "hello"; p11.Sex = "男"; DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType()); string szJson = ""; //序列化 using (MemoryStream stream = new MemoryStream()) { json.WriteObject(stream, p11); szJson = Encoding.UTF8.GetString(stream.ToArray()); Response.Write(szJson); } //反序列化 //using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson))) //{ // DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People)); // Person1 _people = (Person1)serializer.ReadObject(ms); //} } protected void Button1_Click(object sender, EventArgs e) { Response.Write(constr); } }
來自: 昵稱10504424 > 《工作》
0條評論
發(fā)表
請遵守用戶 評論公約
C#實現(xiàn)JSON序列化與反序列化介紹
執(zhí)行序列化反序列化://序列化 using (MemoryStream stream = new MemoryStream()) { json.WriteObject(stream, people); szJson = Encoding.UTF8.GetString(stream.ToArray()); } //反序列化 using (M...
DataContractJsonSerializer類
DataContractJsonSerializer類。} } [DataContract] //對于使用DataContractJsonSerializer類而言,該屬性是必須的 public class Person { public Person(int id,string name,int age) { Id = id; Age ...
對象序列化
對象序列化using System;using System.IO;using System.Linq;using System.Text;using System.Collections.Generic;using System.Runtime.Serialization.Json;using System.Data;using System.Web.Scri...
JSONSerializer.writeWithFieldName
JSONSerializer.writeWithFieldName 如何:對 JSON 數(shù)據(jù)進行序列化和反序列化。NET type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of .本主...
C# 中使用JSON - DataContractJsonSerializer
c# 解析JSON的幾種辦法
// 序列化 var jsonString = JSON.stringify(new[] { p1, p2 }); log(jsonString == JSON.stringify(new List<Person>() { p1, p2 })); //true log(jsonString); // 反序列化...
基于EasyUI的軟件框架打造-數(shù)據(jù)(JSON)封裝
public class Json { private string[] name = new string[]{"javascript","json"};
DataTable<=>List<T>
DataTableListusing System;集合 /// public static DataTable ToDataTable(IList list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetTy...
C#中,Json的序列化和反序列化的幾種方式總結(jié)
C#中,Json的序列化和反序列化的幾種方式總結(jié)。什么是JSON?DataContractJsonSerializer類幫助我們序列化和反序列化Json,他在程序集 Sy...
微信掃碼,在手機上查看選中內(nèi)容