C# .NET 遍历Json 形成键值对

记录一下小方法,在C#中可能用到,获取Json中很多的键值,如果一个一个手敲出来有些麻烦,不过本方法还是会损耗一些性能滴。懒人编程找方法...


            string test_json = "{\"name\":\"tom\",\"nickname\":\"tony\",\"sex\":\"male\",\"age\":20,\"email\":\"[email protected]\"}";
var o = JObject.Parse(yourJsonString);

            foreach (JToken child in o.Children())
            {
                var property1 = child as JProperty;
                MessageBox.Show(property1.Name + ":" + property1.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);
                        }
                    }
                }
            }


猜你喜欢

转载自blog.csdn.net/sinat_30224769/article/details/51702203