C#将对象序列化成JSON字符串

  1. public string GetJsonString()
  2. {
  3. List<Product> products = new List<Product>(){
  4. new Product(){Name="苹果",Price=5.5},
  5. new Product(){Name="橘子",Price=2.5},
  6. new Product(){Name="干柿子",Price=16.00}
  7. };
  8. ProductList productlist = new ProductList();
  9. productlist.GetProducts = products;
  10. return new JavaScriptSerializer().Serialize(productlist));
  11. }
  12. public class Product
  13. {
  14. public string Name { get; set; }
  15. public double Price { get; set; }
  16. }
  17. public class ProductList
  18. {
  19. public List<Product> GetProducts { get; set; }
  20. }

这里主要是使用JavaScriptSerializer来实现序列化操作,这样我们就可以把对象转换成Json格式的字符串,生成的结果如下:

  1. {"GetProducts":[{"Name":"苹果","Price":5.5},{"Name":"橘子","Price":2.5},{"Name":"柿子","Price":16}]}

如何将Json字符串转换成对象使用呢?

在实际开发中,经常有可能遇到用JS传递一个Json格式的字符串到后台使用,如果能自动将字符串转换成想要的对象,那进行遍历或其他操作时,就方便多了。那具体是如何实现的呢?

  1. public static List<T> JSONStringToList<T>(this string JsonStr)
  2. {
  3. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  4. List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
  5. return objs;
  6. }
  7. public static T Deserialize<T>(string json)
  8. {
  9. T obj = Activator.CreateInstance<T>();
  10. using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  11. {
  12. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  13. return (T)serializer.ReadObject(ms);
  14. }
  15. }
  16. string JsonStr = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";
  17. List<Product> products = new List<Product>();
  18. products = JSONStringToList<Product>(JsonStr);
  19. foreach (var item in products)
  20. {
  21. Response.Write(item.Name + ":" + item.Price + "<br />");
  22. }
  23. public class Product
  24. {
  25. public string Name { get; set; }
  26. public double Price { get; set; }
  27. }

在上面的例子中,可以很方便的将Json字符串转换成List对象,操作的时候就方便多了~

[参考]C# JSON字符串序列化与反序列化的更多相关文章

  1. C# JSON字符串序列化与反序列化

    JSON与c#对象转换http://hi.baidu.com/donick/item/4d741338870c91fe97f88d33 C# JSON字符串序列化与反序列化 – http://www. ...

  2. C# JSON字符串序列化与反序列化常见模型举例

    C#中实体转Json常用的类JavaScriptSerializer,该类位于using System.Web.Script.Serialization;命名空间中,添加引用system.web.ex ...

  3. JSON字符串序列化与反序列化浅试

    一.添加引用(using Newtonsoft.Json.Linq;) 二. 1.生成json字符串源码 List<string> list = new List<string> ...

  4. Newtonsoft.Json 的序列化与反序列化

    首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framework和NHibernate的.我举例说明DataTable的序列化和反序列化.创建一 ...

  5. 【转】Newtonsoft.Json 的序列化与反序列化

    http://www.cnblogs.com/08shiyan/p/3464028.html 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Fr ...

  6. Json.Net序列化和反序列化设置

    首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framework和NHibernate的.我举例说明DataTable的序列化和反序列化.创建一 ...

  7. Json.Net系列教程 3.Json.Net序列化和反序列化设置

    原文 Json.Net系列教程 3.Json.Net序列化和反序列化设置 上节补充 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framewo ...

  8. 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

  9. Json的序列化与反序列化以及乱入的k_BackingField

    0.Newtonsoft.json 最简单的最强大的基于c#的json解析库是Newtonsoft.json 在NuGet程序包管理器中在线搜索“json”,选择JSon.Net,并安装.   使用到 ...

随机推荐

  1. 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 ...

  2. Java语言基础及java核心

    一.Java语言特点 1. 简单 2. 面向对象 3. 分布式 4. 健壮 5. 安全 6. 中性架构跨平台 7. 超强的可移植性 8. 高性能 9. 多线程 二.java的环境变量 JAVA_HOM ...

  3. 14.Jmeter聚合报告各项含义

    Aggregate Report 是 JMeter 常用的一个 Listener,中文为“聚合报告” Label:每个 JMeter 的 element(例如 HTTP Request)都有一个 Na ...

  4. shell编程:变量替换

    定义变量:$ var_1="I love you, Do you love me" 输出变量:$ echo $var_1 打印结果:I love you, Do you love ...

  5. 截取url参数

    //获得参数(只对字母数字等有效,参数值为中文则不能传) function getQueryString(name) { var reg = new RegExp("(^|&)&qu ...

  6. 挂载时出现mount: RPC: Unable to receive; errno = Connection refused错误的解决方法

    当我们在做NFS开发板下挂载时,经常会出现mount: RPC: Unable to receive; errno = Connection refused的错误,连接被拒绝了,到底是什么原因呢? 这 ...

  7. Spring学习笔记(1)——初识Spring

    一.Spring是什么       通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大 ...

  8. LeetCode Array Easy169. Majority Element

    Description Given an array of size n, find the majority element. The majority element is the element ...

  9. js浮点金额计算精度

    在js中进行以元为单位进行浮点数计算时,会产生精度问题,例如: console.log(0.1+0.2) 结果为:0.30000000000000004 大多数编程语言计算采用的是IEEE 754 标 ...

  10. Lenovo E42-80安装Linux的注意事项

    Lenovo E42-80安装Linux的注意事项 https://www.cnblogs.com/dylanchu/p/9750760.html 1. 用U盘做个liveCD While makin ...