日期:2014-05-17  浏览次数:20465 次

对JSON不懂,直接求解解方案。
功能描述:用户输入一个IP地址,查IP所在地等信息。

API接口:http://ip.taobao.com/service/getIpInfo.php?ip=125.36.12.14

接口返回数据是JSON:
{"code":0,"data":{"ip":"125.36.12.14","country":"\u4e2d\u56fd","country_id":"86","area":"\u534e\u5317","area_id":"100000","region":"\u5929\u6d25\u5e02","region_id":"120000","city":"\u5929\u6d25\u5e02","city_id":"120000","county":"\u6b66\u6e05\u533a","county_id":"120114","isp":"\u8054\u901a","isp_id":"100026"}}

code中的0表示成功,1表示失败。

对JSON不懂,用string接收不了这段数据。

所以直接求代码,最好能有注释。

------解决方案--------------------
用 Json.NET 反序列化成

public class IpInfo
{
public string code;
public Hashtable data;
}

C# code
class Program
    {
        static void Main(string[] args)
        {
            var getIpInfoUrl = "http://ip.taobao.com/service/getIpInfo.php?ip={0}";
            getIpInfoUrl = string.Format(getIpInfoUrl, "125.36.12.14");

            var client = new WebClient();
            var result = client.DownloadString(getIpInfoUrl);

            var ipInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<IpInfo>(result);
            foreach (var key in ipInfo.data.Keys)
                Console.WriteLine("{0}:{1}", key, ipInfo.data[key]);

            Console.Read();
        }
    }

    public class IpInfo
    {
        public string code;
        public Hashtable data;
    }

------解决方案--------------------
C# code
    protected void Page_Load(object sender, EventArgs e)
    {
        string path = "http://ip.taobao.com/service/getIpInfo.php?ip=125.36.12.14";
        HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(path);
        HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();
        Stream stream = null;
        try
        {
            stream = hwrp.GetResponseStream();
            DataContractJsonSerializer datajson = new DataContractJsonSerializer(typeof(IpInfo));
            IpInfo ipinfo = datajson.ReadObject(stream) as IpInfo;
            Response.Write(ipinfo.code + "==" + ipinfo.data.ip + "==" + ipinfo.data.country + "==" + ipinfo.data.area);
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
                stream.Dispose();
            }
        }
    }
    [DataContract]
    public class IpInfo
    {
        [DataMember]
        public int code;
        [DataMember]
        public data data;
    }
    [DataContract]
    public class data
    {
        [DataMember]
        public string ip;
        [DataMember]
        public string country;
        [DataMember]
        public string country_id;
        [DataMember]
        public string area;
        [DataMember]
        public string area_id;
        [DataMember]
        public string region;
        [DataMember]
        public string region_id;
        [DataMember]
        public string city;
        [DataMember]
        public string city_id;
        [DataMember]
        public string county;
        [DataMember]
        public string county_id;
        [DataMember]
        public string isp;
        [DataMember]
        public string isp_id;
    }