今天同事问我关于 .net解析json的问题, 我们都知道java中使用阿里开源的fastjson解析json挺方便的,net中也有一个类似的解析json的方法
下载地址:http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=json&DownloadId=1648351&FileTime=131346059033730000&Build=21053
一个简单的例子:
static void Main(string[] args) { string strJson = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }"; JObject jObject = JObject.Parse(strJson); string userName = (string)jObject["Name"]; Console.WriteLine(userName);//CPU JArray jsonColleagues = (JArray)jObject["Colleagues"]; Console.WriteLine(jsonColleagues.Count()); Console.WriteLine(jsonColleagues.Count); Console.WriteLine((string)((JArray)jObject["Colleagues"])[0]["Name"]); Console.WriteLine((string)jsonColleagues[0]["Name"]); Console.Read(); }