C# Json解析筆記

C# Json解析

Json數據格式:json

{"code":200,"data":{"ipaddress":"127.0.0.1","cuntry":"本機地址","local":"CZ88.NET"},"msg":"request success"}code

IPInfoModel示範案列:orm

class IpInfoModel
    {

        public string code
        {
            get; set;
        }

        public Data data
        {
            get; set;
        }

        public string msg
        {
            get; set;
        }

    }

    public class Data
    {
        public string ipaddress
        {
            get; set;
        }
        public string cuntry
        {
            get; set;
        }

        public string local
        {
            get; set;
        }
    }

解析代碼案列:ip

public string JsonParse(string jsonRes)
        {
            // Console.WriteLine(jsonRes);

            string ipinfoRes = "";
            //Console.WriteLine(ipifo.code);
            if (jsonRes != "")
            {
                IpInfoModel ipinfo = JsonConvert.DeserializeObject<IpInfoModel>(jsonRes);
                ipinfoRes = string.Format(" Area:{0}  Local:{1}", ipinfo.data.cuntry, ipinfo.data.local);
                return ipinfoRes;
            }
            else
            {
                return "plz try again.";
            }
            return ipinfoRes;
        }
相關文章
相關標籤/搜索