C#實(shí)現(xiàn)json的序列化和反序列化實(shí)例代碼
在做asp.net和unity進(jìn)行http通信的時(shí)候,當(dāng)unity客戶端發(fā)出表單請(qǐng)求的時(shí)候,我要將他要請(qǐng)求的數(shù)據(jù)以json的格式返回給客戶端,讓客戶端來(lái)解析。服務(wù)器端這一塊就涉及到j(luò)son的序列化和反序列化的問(wèn)題。
兩種方法都有例子,第一種是用泛型集合來(lái)存儲(chǔ)的對(duì)象,然后將集合序列化一下;第二種是直接序列化的一個(gè)對(duì)象
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類實(shí)現(xiàn)簡(jiǎ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類實(shí)現(xiàn)序列化
//可以使用IgnoreDataMember:指定該成員不是數(shù)據(jù)協(xié)定的一部分且沒(méi)有進(jìn)行序列化,DataMember:定義序列化屬性參數(shù),使用DataMember屬性標(biāo)記字段必須使用DataContract標(biāo)記類 否則DataMember標(biāo)記不起作用。
[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);
}
}
- C#實(shí)現(xiàn)XML序列化與反序列化
- C#對(duì)Json進(jìn)行序列化和反序列化
- C#中關(guān)于序列化與反序列化的三種方法
- C# 解析XML和反序列化的示例
- C# Newtonsoft.Json 解析多嵌套json 進(jìn)行反序列化的實(shí)例
- C#中Json反序列化的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)XML與實(shí)體類之間相互轉(zhuǎn)換的方法(序列化與反序列化)
- C#序列化與反序列化(Serialize,Deserialize)實(shí)例詳解
- c#對(duì)象反序列化與對(duì)象序列化示例詳解
- 深入理解C#序列化與反序列化的詳解
- C# SimpleJSON字典反序列化實(shí)戰(zhàn)教程
相關(guān)文章
C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04C# 常用協(xié)議實(shí)現(xiàn)模版及FixedSizeReceiveFilter示例(SuperSocket入門)
本文主要介紹了常用協(xié)議實(shí)現(xiàn)模版及FixedSizeReceiveFilter示例。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01