在C#中可能用到,获取Json中很多的键值,如果一个一个手敲出来有些麻烦,不过本方法还是会损耗一些性能滴。懒人编程找方法…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using Newtonsoft.Json.Linq; //单层Json string json = "{\"name\":\"tom\",\"nickname\":\"tony\",\"sex\":\"male\",\"age\":20,\"email\":\"123@123.com\"}"; var o = JObject.Parse(json); foreach (var item in o.Children()) { var p = item as JProperty; string s = p.Name + ":" + p.Value; } //多层Json var o = JObject.Parse(yourJsonString); foreach (JToken child in o.Children()) { //var property1 = child as JProperty; //MessageBox.Show(property1.Name + ":" + property1.Value); foreach (JToken grandChild in child) { foreach (JToken grandGrandChild in grandChild) { var property = grandGrandChild as JProperty; if (property != null) { MessageBox.Show(property.Name + ":" + property.Value); } } } } |