【问题标题】:Get value from json data using c#使用 c# 从 json 数据中获取值
【发布时间】:2015-07-08 12:21:48
【问题描述】:

我正在使用 C# 创建一个 Windows 窗体应用程序项目

我想在我的文本框中显示与 IP 地址相关的所有信息(如不同标签中的城市名称、国家代码等)。 我已经阅读了很多关于JsonConvert 的文章,我不想使用JsonConvert

这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace GetIPinfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<LocaionInfo1> locations = new List<LocaionInfo1>();
            string url = string.Format("http://ipinfo.io/" + txtip.Text);
            using (WebClient client = new WebClient())
            {
                string json = client.DownloadString(url);
                LocaionInfo1 location = new JavaScriptSerializer().Deserialize<LocaionInfo1>(json);
                locations.Add(location);
            }

            if (locations.Count > 0)
            {
                foreach (LocaionInfo1 loc in locations)
                {
                    label9.Text = loc.CityName;
                    label10.Text = loc.CountryCode;
                    label11.Text = loc.CountryName;
                }
            }
        }

        public class LocaionInfo1
        {
            public string IPAddress { get; set; }
            public string CountryName { get; set; }
            public string CountryCode { get; set; }
            public string CityName { get; set; }
            public string RegionName { get; set; }
            public string ZipCode { get; set; }
            public string Latitude { get; set; }
            public string Longitude { get; set; }
            public string TimeZone { get; set; }
        }
    }
}

问题是当我调试这段代码并在我的文本框中输入 IP 然后点击提交按钮 我的LocaionInfo1 location = new JavaScriptSerializer().Deserialize&lt;LocaionInfo1&gt;(json);NULL 值。

JSON 数据是:

{
  "ip": "182.69.151.41",
  "hostname": "abts-north-dynamic-041.151.69.182.airtelbroadband.in",
  "city": null,
  "country": "IN",
  "loc": "20.0000,77.0000",
  "org": "AS24560 Bharti Airtel Ltd., Telemedia Services"
}

所以请帮助获取这些所有值。 我正在使用 Visual Studio 2012。

【问题讨论】:

    标签: c# asp.net json json.net


    【解决方案1】:

    所以我实际上无法尝试此操作,因为我在移动设备上,但对于实际的 JSON,您必须在链接中添加一个“/json”

    string url = string.Format("http://ipinfo.io/" + txtip.Text + "/json");
    

    【讨论】:

      【解决方案2】:

      您需要通过在对象属性上使用属性提供一些映射来帮助 json.net:

          public class LocaionInfo1
          {
              [JsonProperty(PropertyName = "ip")]
              public string IPAddress { get; set; }
              ....
          }
      

      【讨论】:

        【解决方案3】:

        您的属性名称不一致。例如,在 JSON 中,您有“city”,但在您的对象中,它是“CityName”。反序列化器应该如何知道如何映射它们?

        【讨论】:

        • 为了防止这种不一致,我建议使用类似“json2csharp.com”的东西。让您的生活更轻松。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        相关资源
        最近更新 更多