[参考]C# JSON字符串序列化与反序列化
C#将对象序列化成JSON字符串
- public string GetJsonString()
- {
- List<Product> products = new List<Product>(){
- new Product(){Name="苹果",Price=5.5},
- new Product(){Name="橘子",Price=2.5},
- new Product(){Name="干柿子",Price=16.00}
- };
- ProductList productlist = new ProductList();
- productlist.GetProducts = products;
- return new JavaScriptSerializer().Serialize(productlist));
- }
- public class Product
- {
- public string Name { get; set; }
- public double Price { get; set; }
- }
- public class ProductList
- {
- public List<Product> GetProducts { get; set; }
- }
这里主要是使用JavaScriptSerializer来实现序列化操作,这样我们就可以把对象转换成Json格式的字符串,生成的结果如下:
- {"GetProducts":[{"Name":"苹果","Price":5.5},{"Name":"橘子","Price":2.5},{"Name":"柿子","Price":16}]}
如何将Json字符串转换成对象使用呢?
在实际开发中,经常有可能遇到用JS传递一个Json格式的字符串到后台使用,如果能自动将字符串转换成想要的对象,那进行遍历或其他操作时,就方便多了。那具体是如何实现的呢?
- public static List<T> JSONStringToList<T>(this string JsonStr)
- {
- JavaScriptSerializer Serializer = new JavaScriptSerializer();
- List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
- return objs;
- }
- public static T Deserialize<T>(string json)
- {
- T obj = Activator.CreateInstance<T>();
- using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
- {
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
- return (T)serializer.ReadObject(ms);
- }
- }
- string JsonStr = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";
- List<Product> products = new List<Product>();
- products = JSONStringToList<Product>(JsonStr);
- foreach (var item in products)
- {
- Response.Write(item.Name + ":" + item.Price + "<br />");
- }
- public class Product
- {
- public string Name { get; set; }
- public double Price { get; set; }
- }
在上面的例子中,可以很方便的将Json字符串转换成List对象,操作的时候就方便多了~
[参考]C# JSON字符串序列化与反序列化的更多相关文章
- C# JSON字符串序列化与反序列化
JSON与c#对象转换http://hi.baidu.com/donick/item/4d741338870c91fe97f88d33 C# JSON字符串序列化与反序列化 – http://www. ...
- C# JSON字符串序列化与反序列化常见模型举例
C#中实体转Json常用的类JavaScriptSerializer,该类位于using System.Web.Script.Serialization;命名空间中,添加引用system.web.ex ...
- JSON字符串序列化与反序列化浅试
一.添加引用(using Newtonsoft.Json.Linq;) 二. 1.生成json字符串源码 List<string> list = new List<string> ...
- Newtonsoft.Json 的序列化与反序列化
首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framework和NHibernate的.我举例说明DataTable的序列化和反序列化.创建一 ...
- 【转】Newtonsoft.Json 的序列化与反序列化
http://www.cnblogs.com/08shiyan/p/3464028.html 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Fr ...
- Json.Net序列化和反序列化设置
首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framework和NHibernate的.我举例说明DataTable的序列化和反序列化.创建一 ...
- Json.Net系列教程 3.Json.Net序列化和反序列化设置
原文 Json.Net系列教程 3.Json.Net序列化和反序列化设置 上节补充 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framewo ...
- 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型;
导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型: 二:C#对象.集合.DataTable与Json内容互转示例: ...
- Json的序列化与反序列化以及乱入的k_BackingField
0.Newtonsoft.json 最简单的最强大的基于c#的json解析库是Newtonsoft.json 在NuGet程序包管理器中在线搜索“json”,选择JSon.Net,并安装. 使用到 ...
随机推荐
- Mysql create constraint foreign key faild.trouble shooting method share
mysql> create table tb_test (id int(10) not null auto_increment primary key,action_id int(10) not ...
- Java语言基础及java核心
一.Java语言特点 1. 简单 2. 面向对象 3. 分布式 4. 健壮 5. 安全 6. 中性架构跨平台 7. 超强的可移植性 8. 高性能 9. 多线程 二.java的环境变量 JAVA_HOM ...
- 14.Jmeter聚合报告各项含义
Aggregate Report 是 JMeter 常用的一个 Listener,中文为“聚合报告” Label:每个 JMeter 的 element(例如 HTTP Request)都有一个 Na ...
- shell编程:变量替换
定义变量:$ var_1="I love you, Do you love me" 输出变量:$ echo $var_1 打印结果:I love you, Do you love ...
- 截取url参数
//获得参数(只对字母数字等有效,参数值为中文则不能传) function getQueryString(name) { var reg = new RegExp("(^|&)&qu ...
- 挂载时出现mount: RPC: Unable to receive; errno = Connection refused错误的解决方法
当我们在做NFS开发板下挂载时,经常会出现mount: RPC: Unable to receive; errno = Connection refused的错误,连接被拒绝了,到底是什么原因呢? 这 ...
- Spring学习笔记(1)——初识Spring
一.Spring是什么 通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大 ...
- LeetCode Array Easy169. Majority Element
Description Given an array of size n, find the majority element. The majority element is the element ...
- js浮点金额计算精度
在js中进行以元为单位进行浮点数计算时,会产生精度问题,例如: console.log(0.1+0.2) 结果为:0.30000000000000004 大多数编程语言计算采用的是IEEE 754 标 ...
- Lenovo E42-80安装Linux的注意事项
Lenovo E42-80安装Linux的注意事项 https://www.cnblogs.com/dylanchu/p/9750760.html 1. 用U盘做个liveCD While makin ...