在C#中可以对JSON进行序列化与反序列化,十分的方便。代码如下:
首先引入命名空间:using System.Web.Script.Serialization;
再定义一个类,如下:
public class jsonClass { public string id; public string name; public string pId; public string isParent; public string open; public string senable; public string CHECKED; public string chkDisabled; }
将一个JSON字符串转换成jsonClass类实例,如下:
JavaScriptSerializer serializer = new JavaScriptSerializer(); List<jsonClass> jsonClassList = serializer.Deserialize<List<jsonClass>>(jsonString);
将List<jsonClass>转换成json
string resultString = serializer.Serialize(jsonClassList);