C#JSON从List<>获取数据

时间:2017-05-07 16:20:23

标签: c# json uwp

我正在尝试制作简单的UWP天气应用程序,仅用于学习目的,我无法从JSON获取数据。

如何从公共类ConsolidatedWeather获得最低和最高温度? 我可以从其他课程中获取数据。

非常感谢

Vrime.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;


namespace _01_Weaather
{

class Vrime
{

    public async static Task<ConsolidatedWeather> ShowTemp()
    {
        var http = new HttpClient();
        var url = String.Format("https://www.metaweather.com/api/location/44418/");
        var response = await http.GetAsync(url);
        var result = await response.Content.ReadAsStringAsync();           
        var ser = new DataContractJsonSerializer(typeof(ConsolidatedWeather));
        var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
        var data = (ConsolidatedWeather)ser.ReadObject(ms);

        return data;
    }
}

[DataContract]
public class ConsolidatedWeather
{
    [DataMember]
    public object id { get; set; }
    [DataMember]
    public string weather_state_name { get; set; }
    [DataMember]
    public string weather_state_abbr { get; set; }
    [DataMember]
    public string wind_direction_compass { get; set; }
    [DataMember]
    public string created { get; set; }
    [DataMember]
    public string applicable_date { get; set; }
    [DataMember]
    public double min_temp { get; set; }
    [DataMember]
    public double max_temp { get; set; }
    [DataMember]
    public double the_temp { get; set; }
    [DataMember]
    public double wind_speed { get; set; }
    [DataMember]
    public double wind_direction { get; set; }
    [DataMember]
    public double air_pressure { get; set; }
    [DataMember]
    public int humidity { get; set; }
    [DataMember]
    public double? visibility { get; set; }
    [DataMember]
    public int predictability { get; set; }
}
[DataContract]
public class Parent
{
    [DataMember]

    public string title { get; set; }
    [DataMember]
    public string location_type { get; set; }
    [DataMember]
    public int woeid { get; set; }
    [DataMember]
    public string latt_long { get; set; }
}
[DataContract]
public class Source
{
    [DataMember]
    public string title { get; set; }
    [DataMember]
    public string slug { get; set; }
    [DataMember]
    public string url { get; set; }
    [DataMember]
    public int crawl_rate { get; set; }
}
[DataContract]
public class RootObject
{[DataMember]
    public List<ConsolidatedWeather> consolidated_weather { get; set; }
    [DataMember]
    public string time { get; set; }
    [DataMember]
    public string sun_rise { get; set; }
    [DataMember]
    public string sun_set { get; set; }
    [DataMember]
    public string timezone_name { get; set; }
    [DataMember]
    public Parent parent { get; set; }
    [DataMember]
    public List<Source> sources { get; set; }
    [DataMember]
    public string title { get; set; }
    [DataMember]
    public string location_type { get; set; }
    [DataMember]
    public int woeid { get; set; }
    [DataMember]
    public string latt_long { get; set; }
    [DataMember]
    public string timezone { get; set; }
}

MainPage.xaml中

 namespace _01_Weaather
 {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    double min;
    double max;


    public MainPage()
    {
        this.InitializeComponent();



    }

    private async void BtnPrikaz_OnClick(object sender, RoutedEventArgs e)
    {
       ConsolidatedWeather cWeather = await Vrime.ShowTemp();
        min =cWeather.min_temp;
        max = cWeather.max_temp;
        txtTemp.Text = String.Format(min.ToString() + "\n"+ max.ToString());


    }

}

1 个答案:

答案 0 :(得分:1)

尝试更改属性min&amp;的数据类型浮动的最大温度,您使用错误的对象类型进行反序列化。使用你的根对象。此外,我建议使用像restsharp这样的东西来消费api。这将使你的生活更轻松2倍。