获取json字段的key和value

1、获取对应key的值

JObject jt = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>("{\"test1\":\"aaaa\",\"test2\":\"bbbbbb\"}");
            Console.Write(jt["test1"].ToString());
            Console.Write(jt["test2"].ToString());

2、获取key和value

 string json = "{\"a\":\"a\",\"s\":\"s\",\"d\":\"f\",\"f\":f}";
            var o = JObject.Parse(json);
            foreach (var item in o.Children())
            {
                var p = item as JProperty;
                string s = p.Name + ":" + p.Value;
              
            }
 string json = "{\"a\":[{\"a\":\"a\",\"s\":\"s\",\"d\":\"d\",\"f\":f},{\"a\":\"a\",\"s\":\"s\",\"d\":\"d\",\"f\":f},{\"a\":\"a\",\"s\":\"s\",\"d\":\"d\",\"f\":f}]}";
            var o = JObject.Parse(json);
            Console.WriteLine(o);
            Console.Read();
            foreach (JToken child in o.Children())
            {

                foreach (JToken grandChild in child)
                {

                    foreach (JToken grandGrandChild in grandChild)
                    {

                        foreach (var item in grandGrandChild.Children())
                        {
                            var p = item as JProperty;
                            string s = p.Name + ":" + p.Value;
                           
                         
                        }
                       
                    }
                }
            }

 

posted @ 2020-03-14 16:38  越吃越瘦  阅读(2990)  评论(0编辑  收藏  举报