发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
在做asp.net和unity进行http通信的时候,当unity客户端发出表单请求的时候,我要将他要请求的数据以json的格式返回给客户端,让客户端来解析。服务器端这一块就涉及到json的序列化和反序列化的问题。
接下来就来举个列子,当然包括两种方法(本篇参考自: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类实现简单的序列化 [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类实现序列化 //可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化,DataMember:定义序列化属性参数,使用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条评论
发表
请遵守用户 评论公约
C#实现JSON序列化与反序列化介绍
执行序列化反序列化://序列化 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 数据进行序列化和反序列化。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的软件框架打造-数据(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的序列化和反序列化的几种方式总结
C#中,Json的序列化和反序列化的几种方式总结。什么是JSON?DataContractJsonSerializer类帮助我们序列化和反序列化Json,他在程序集 Sy...
微信扫码,在手机上查看选中内容