Newtonsoft.dll插件
洪大师二次封装:
using System;using System.Collections.Generic;using System.Text;using System.Data;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace DevFramework_2015.Common{ public class JsonHelper { public static string ListToJson(List li) #region { string jsonString = ""; jsonString = JsonConvert.SerializeObject(li, Formatting.None); return jsonString; } #endregion public static List JsonToList (string jsonString) #region { List li = JsonConvert.DeserializeObject
>(jsonString); return li; } #endregion public static string DictionaryToJson (Dictionary dic) #region { string jsonString = ""; jsonString = JsonConvert.SerializeObject(dic, Formatting.None); return jsonString; } #endregion public static Dictionary JsonToDictionary (string jsonString) #region { Dictionary dic = JsonConvert.DeserializeObject >(jsonString); return dic; } #endregion public static string ArrayToJson (T[] arrayObject) #region { string jsonString = ""; jsonString = JsonConvert.SerializeObject(arrayObject, Formatting.None); return jsonString; } #endregion public static T[] JsonToArray (string jsonString) #region { T[] arrayObject = JsonConvert.DeserializeObject (jsonString); return arrayObject; } #endregion public static string DataTableToJson(DataTable dt) #region { string jsonString = ""; jsonString = JsonConvert.SerializeObject(dt, Formatting.None); return jsonString; } #endregion public static DataTable JsonToDataTable(string jsonString) #region { DataTable dt; dt = JsonConvert.DeserializeObject (jsonString); return dt; } #endregion }}
对象直接转化为json字符串:
string jsonstr = JsonConvert.SerializeObject(response)
json字符串转化为已知的对象
Response_QueryPrepareInfo _Response_QueryPrepareInfo = JsonConvert.DeserializeObject(sRetStr);