JSON数据的两种读写方法

3710阅读 0评论2016-03-14 dyli2000
分类:HTML5

1、利用.NET自身的JavaScriptSerializer

需要添加System.Web.Extensions.dll

添加方法见:

http://blog.chinaunix.net/uid-25498312-id-5675200.html


点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Script.Serialization;
  6. namespace TestJSON
  7. {
  8.     class CustomData
  9.     {
  10.         public string Input;
  11.         public string Output;
  12.     }
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.WriteLine("----------------------- Using .Net JavaScriptSerializer api for JSON ------------------------\n");
  18.             CustomData p = new CustomData() { Input = "stone", Output = "gold" };
  19.             JavaScriptSerializer serializer = new JavaScriptSerializer();
  20.             var json = serializer.Serialize(p);
  21.             Console.WriteLine(json);
  22.             var p1 = serializer.Deserialize(json);
  23.             Console.WriteLine(p1.Input + "=>" + p1.Output);
  24.             // 确定指定的 System.Object 实例是否是相同的实例
  25.             Console.WriteLine(ReferenceEquals(p, p1));
  26.             Console.ReadLine();
  27.         }
  28.     }
  29. }

image

上述结果同时证明了从p到p1是深拷贝。


2、利用Newtonsoft.Json.dll开源库

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Script.Serialization;
  6. namespace TestJSON
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("----------------------- Using NewtonSoftJson api for JSON ------------------------\n");
  13.             //匿名对象解析,uid=0即为整型,若uid="0"则为字符串 
  14.             var tempEntity = new { uid = 0, rid = 0, cmd = 0, commander = 0, target = 0 };
  15.             // 序列化的后发送
  16.             string jsonStr = JsonHelper.SerializeObject(tempEntity);
  17.             // 收到后解析
  18.             tempEntity = JsonHelper.DeserializeAnonymousType("{\"uid\":123,\"rid\":466,\"cmd\":4099,\"commander\":123,\"target\":666}", tempEntity);
  19.             Console.WriteLine(tempEntity.uid);
  20.             Console.ReadLine();
  21.         }
  22.     }
  23. }


  1. /* JasonHelper.cs文件: */
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using Newtonsoft.Json;

  8. namespace TestJSON
  9. {
  10.     ///
  11.     /// Json帮助类
  12.     ///
  13.     public class JsonHelper
  14.     {
  15.         ///
  16.         /// 将对象序列化为JSON格式
  17.         ///
  18.         /// 对象
  19.         /// json字符串
  20.         public static string SerializeObject(object o)
  21.         {
  22.             string json = JsonConvert.SerializeObject(o);
  23.             return json;
  24.         }

  25.         ///
  26.         /// 解析JSON字符串生成对象实体
  27.         ///
  28.         /// 对象类型
  29.         /// json字符串(eg.{"ID":"112","Name":"石子儿"})
  30.         /// 对象实体
  31.         public static T DeserializeJsonToObject(string json) where T : class
  32.         {
  33.             JsonSerializer serializer = new JsonSerializer();
  34.             StringReader sr = new StringReader(json);
  35.             object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
  36.             T t = o as T;
  37.             return t;
  38.         }


  39.         ///
  40.         /// 解析JSON数组生成对象实体集合
  41.         ///
  42.         /// 对象类型
  43.         /// json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])
  44.         /// 对象实体集合
  45.         public static List DeserializeJsonToList(string json) where T : class
  46.         {
  47.             JsonSerializer serializer = new JsonSerializer();
  48.             StringReader sr = new StringReader(json);
  49.             object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List));
  50.             List list = o as List;
  51.             return list;
  52.         }

  53.         ///
  54.         /// 反序列化JSON到给定的匿名对象.
  55.         ///
  56.         /// 匿名对象类型
  57.         /// json字符串
  58.         /// 匿名对象
  59.         /// 匿名对象
  60.         public static T DeserializeAnonymousType(string json, T anonymousTypeObject)
  61.         {
  62.             T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
  63.             return t;
  64.         }
  65.     }
  66. }


image


工程源码:
TestJSON.rar
Newtonsoft.Json.dll库:
Newtonsoft.rar

参考文献:

http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html

上一篇:VS 2010程序添加对System.Web.Script.Serialization的引用
下一篇:PS快速画横线竖线斜线