分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

json序列化与反序列化

发布时间:2023-09-06 01:22责任编辑:傅花花关键词:jsjson

方法1 :

需要手工添加引用:System.Web.Extensions,使用较方便:

 Dictionary<string, string> dict = new Dictionary<string, string>();
            dict["s"] = "s1";
            dict["t"] = "t1";
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = serializer.Serialize(dict);

            dict = serializer.Deserialize<Dictionary<string, string>>(json);

方法2:

不需手工添加,但使用较复杂

 Dictionary<string, string> dict = new Dictionary<string, string>();
            dict["s"] = "s1";
            dict["t"] = "t1";

            System.Runtime.Serialization.Json.DataContractJsonSerializer p = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Dictionary<string, string>));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            p.WriteObject(ms, dict);
            string str = System.Text.Encoding.UTF8.GetString(ms.ToArray());

            byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
            ms = new System.IO.MemoryStream(b);

            dict = p.ReadObject(ms) as Dictionary<string, string>;

方法3:

使用开源组件Newtonsoft.Json(下载地址http://json.codeplex.com/)

另外:json基本也是序列化和反序列化的问题,以下是c#本身的序列化组件

NET框架提供了两种串行化的方式:

1、是使用BinaryFormatter进行串行化;

2、使用SoapFormatter进行串行化;

3、使用XmlSerializer进行串行化。

json序列化与反序列化

原文地址:http://www.cnblogs.com/81/p/7776842.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved