25
07/2014
c#用JObject解析Json
1、下载库:http://json.codeplex.com/
2、添加相应库到项目中。参考:vs2013中c#引用第三方库
3、添加引用:using Newtonsoft.Json.Linq;
4、相关方法:
(1)遍历Json对象
JObject _jObject = JObject.Parse("{'ID':'001','Name':'test','Mark':'Hello Word'}"); foreach (JProperty item in _jObject.Children()) { str.Append(item.Name + "---" + item.Value+"\n"); } /**************************************/ _jObject = JObject.Parse("{ID:{ID1:{ID2:'value',name:'valueName'}},'Name':'test','Mark':'Hello Word'}"); var _value = _jObject["ID"]["ID1"]["ID2"].ToString(); //取值
(2)注意还有数组类型的Json对象,用JArray和JObject配合使用
JArray ja = JArray.Parse(@"[{""Name"":""lj"",""Age"":12}, {""Name"":""cy"",""Age"":28}]"); for (int i = 0; i < ja.Count; i++) { JObject jo = JObject.Parse(ja[i].ToString()); Console.WriteLine(jo["Name"].ToString()); Console.WriteLine(jo["Age"].ToString()); } Console.ReadKey();
转载请注明:康瑞部落 » c#用JObject解析Json
4 条评论