Newtonsoft.Json小记

            /*json相关*/
            //http://www.cnblogs.com/hongfei/p/3593936.html
            string jsonObject = "{\"phone\":\"12345678900\",\"codeType\":\"register\"}";
            JObject jo = JObject.Parse(jsonObject);
            jo.Add("test", "111");//新增
            jo["phone"] = "123456";//修改
            jo.Remove("codeType");//删除
            //JObject遍历json的key和value
            foreach (var item in jo)
            {
                Console.WriteLine(item.Key + ":" + item.Value + ",");
            }
            //JArray遍历
            string jsonArray = "[{'a':'a1','b':'b1'},{'a':'a2','b':'b2'}]";
            JArray ja = JArray.Parse(jsonArray);
            foreach (JObject j in ja)
            {
                foreach (var item in j)
                {
                    Console.WriteLine(item.Key);
                }
            }
            //json嵌套
            string jsonText = "{\"beijing\":{\"zone\":\"海淀\",\"zone_en\":\"haidian\"}}";
            jo = JObject.Parse(jsonText);
            Console.WriteLine("key取值:" + jo["beijing"]["zone"] + ";索引:" + ja[0]["a"]);

 

posted @ 2016-07-06 16:55  苦逼IT男  阅读(237)  评论(0编辑  收藏  举报