【Newtonsoft.Json.dll】操作简单JSON数据

       {
            //json数据
            string json = "{\"name\":\"张三\",\"sex\":\"男\",\"age\":\"20\",\"address\":\"北京\"}";

            //解析数据
            JObject data = JObject.Parse(json);

            //获取各属性的值
            string name = data["name"] + "";
            string sex = data["sex"] + "";
            string age = data["age"] + "";
            string address = data["address"] + "";
        }

        {
            //json数据
            string json = "{\"status\":\"1\",\"data\":{\"name\":\"张三\",\"sex\":\"男\",\"age\":\"20\",\"address\":\"北京\"},\"msg\":\"\"}";

            //解析数据
            JObject data = JObject.Parse(json);

            //如果是成功的状态
            if (data["status"] + "" == "1")
            {
                //获取各属性的值
                string name = data["data"]["name"] + "";
                string sex = data["data"]["sex"] + "";
                string age = data["data"]["age"] + "";
                string address = data["data"]["address"] + "";
            }
            else
                Response.Write("出错:" + data["msg"]);
        }

 

posted @ 2018-09-10 11:16  张伟大  阅读(1459)  评论(0编辑  收藏  举报