【问题标题】:C# How to get data from JSONC#如何从JSON中获取数据
【发布时间】:2019-01-24 16:23:01
【问题描述】:

我从我的网络服务接收到这个 JSON 数据,现在的问题是我目前正试图检索方括号外的数据,因为我当前的代码只能检索 weather 数据。

每当我尝试从 coord 获取数据时,它都会不断返回值 0。

 {  
       "coord":{  
          "lon":145.77,
          "lat":-16.92
       },
       "weather":[  
          {  
             "id":802,
             "main":"Clouds",
             "description":"scattered clouds",
             "icon":"03n"
          }
       ],
       "base":"stations",
       "main":{  
          "temp":300.15,
          "pressure":1007,
          "humidity":74,
          "temp_min":300.15,
          "temp_max":300.15
       },
       "visibility":10000,
       "wind":{  
          "speed":3.6,
          "deg":160
       },
       "clouds":{  
          "all":40
       },
       "dt":1485790200,
       "sys":{  
          "type":1,
          "id":8166,
          "message":0.2064,
          "country":"AU",
          "sunrise":1485720272,
          "sunset":1485766550
       },
       "id":2172797,
       "name":"Cairns",
       "cod":200
    }

这是我当前的代码,我正在尝试从大括号 coord 中获取数据,但它一直给出 0。任何帮助将不胜感激!

public partial class Book : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public class Result
    {
        public string id{ get; set; }
        public string main{ get; set; }
        public string description{ get; set; }
        public string icon{ get; set; }
        public decimal lon{ get; set; }

    }

    public class SearchList
    {
        public int resultCount;
        public Result[] weather;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string searchTerm = TextBox1.Text;
        var webRequest = (HttpWebRequest)WebRequest.Create
        ("http://samples.openweathermap.org/data/2.5/weather?id=2172797&appid=b6907d289e10d714a6e88b30761fae22");
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        if (webResponse.StatusCode == HttpStatusCode.OK)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            string resString = sr.ReadToEnd();
            SearchList list = json.Deserialize<SearchList>(resString);
            GridView1.DataSource = list.weather;
            GridView1.DataBind();
        }
        else
            Label1.Text = "Invalid Response";
    }
}

【问题讨论】:

  • 更新SearchList 定义以包含其他所需数据
  • @Nkosi 我该怎么做你能告诉我吗?

标签: c# json web-services


【解决方案1】:

您的模型不完整。您可以使用 JsonCsharp 生成类。

public class Coord
{
    public double lon { get; set; }
    public double lat { get; set; }
}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class SearchList
{
    public Coord coord { get; set; }
    public List<Weather> weather { get; set; }
    public int resultCount;
}

【讨论】:

  • 当我添加您的代码时,它仍然只显示weather 数据。我还想这样做吗? SearchList list = json.Deserialize&lt;SearchList&gt;(resString); GridView1.DataSource = list.weather;
  • 您将 dataSource 设置为 list.weather。您需要设置 DataSource= list ,同时拥有天气和坐标
  • 你的意思是这样GridView1.DataSource = list;它仍然给我一个错误data source is an invalid type
  • @BestJeanist 根据您的回复,这看起来像XY problem。您需要在原始问题中澄清实际问题是什么
  • @BestJeanist 正如 Nkosi 所说,您现在遇到了不同的问题。在对象列表中,您有您的坐标对象和天气列表。这是你的问题。现在您需要调整绑定以显示天气列表和坐标类型的对象。
【解决方案2】:

您需要为coord添加模型

public class SearchList
{
    public Coord coord;
    public int resultCount;
    public Result[] weather;
}

public class Coord 
{
    public double lon;
    public double lat;
}

您需要为每种类型的数据添加更多模型。你不能简单地从list.weather.lon访问list.coord.lon

【讨论】:

    【解决方案3】:

    您只能检索天气数据,因为它是在对象模型中定义的。

    更新SearchList 类定义以包含其他所需数据

    public class SearchList {
        public Position coord { get; set; }
        public int resultCount { get; set; }
        public Result[] weather { get; set; }
    }
    
    public class Position {  
        public double lon { get; set; }
        public double lat { get; set; }
    }
    

    这样JavaScriptSerializer 在反序列化 JSON 时就会知道包含它。

    【讨论】:

      【解决方案4】:

      使用 JObject 然后您可以使用

      提取每个 Token
      `Jobject.SelectToken("datayouwant")`.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        相关资源
        最近更新 更多