1、利用.NET自身的JavaScriptSerializer
需要添加System.Web.Extensions.dll
添加方法见:
http://blog.chinaunix.net/uid-25498312-id-5675200.html
点击(此处)折叠或打开
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Script.Serialization;
- namespace TestJSON
- {
- class CustomData
- {
- public string Input;
- public string Output;
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("----------------------- Using .Net JavaScriptSerializer api for JSON ------------------------\n");
- CustomData p = new CustomData() { Input = "stone", Output = "gold" };
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- var json = serializer.Serialize(p);
- Console.WriteLine(json);
- var p1 = serializer.Deserialize(json);
- Console.WriteLine(p1.Input + "=>" + p1.Output);
- // 确定指定的 System.Object 实例是否是相同的实例
- Console.WriteLine(ReferenceEquals(p, p1));
- Console.ReadLine();
- }
- }
- }
2、利用Newtonsoft.Json.dll开源库
点击(此处)折叠或打开
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Script.Serialization;
- namespace TestJSON
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("----------------------- Using NewtonSoftJson api for JSON ------------------------\n");
- //匿名对象解析,uid=0即为整型,若uid="0"则为字符串
- var tempEntity = new { uid = 0, rid = 0, cmd = 0, commander = 0, target = 0 };
- // 序列化的后发送
- string jsonStr = JsonHelper.SerializeObject(tempEntity);
- // 收到后解析
- tempEntity = JsonHelper.DeserializeAnonymousType("{\"uid\":123,\"rid\":466,\"cmd\":4099,\"commander\":123,\"target\":666}", tempEntity);
- Console.WriteLine(tempEntity.uid);
- Console.ReadLine();
- }
- }
- }
- /* JasonHelper.cs文件: */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using Newtonsoft.Json;
-
- namespace TestJSON
- {
- ///
- /// Json帮助类
- ///
- public class JsonHelper
- {
- ///
- /// 将对象序列化为JSON格式
- ///
- /// 对象
- /// json字符串
- public static string SerializeObject(object o)
- {
- string json = JsonConvert.SerializeObject(o);
- return json;
- }
-
- ///
- /// 解析JSON字符串生成对象实体
- ///
- /// 对象类型
- /// json字符串(eg.{"ID":"112","Name":"石子儿"})
- /// 对象实体
- public static T DeserializeJsonToObject(string json) where T : class
- {
- JsonSerializer serializer = new JsonSerializer();
- StringReader sr = new StringReader(json);
- object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
- T t = o as T;
- return t;
- }
-
-
- ///
- /// 解析JSON数组生成对象实体集合
- ///
- /// 对象类型
- /// json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])
- /// 对象实体集合
- public static List DeserializeJsonToList(string json) where T : class
- {
- JsonSerializer serializer = new JsonSerializer();
- StringReader sr = new StringReader(json);
- object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List));
- List list = o as List;
- return list;
- }
-
- ///
- /// 反序列化JSON到给定的匿名对象.
- ///
- /// 匿名对象类型
- /// json字符串
- /// 匿名对象
- /// 匿名对象
- public static T DeserializeAnonymousType(string json, T anonymousTypeObject)
- {
- T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
- return t;
- }
- }
- }
工程源码:
TestJSON.rar
Newtonsoft.Json.dll库:
Newtonsoft.rar
参考文献:
http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html