【问题标题】:Extracting data out of nested Json in c#在c#中从嵌套的Json中提取数据
【发布时间】:2013-11-18 18:53:48
【问题描述】:

我想为 windows phone 开发一个天气应用程序。我创建了一个简单的 UI 用于测试目的。包含一个带有文本框和按钮的网格,用于输入城市名称。第二个网格有不同的标签以在点击按钮后显示天气数据.

我正在使用来自 openweathermap.org 的 api。我用 Json 数据制作了类:

{"coord":{"lon":-120.5,"lat":47.5},"sys":{"message":0.195,"country":"US","sunrise":1384787560,"sunset":1384820527},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"base":"gdps stations","main":{"temp":277.15,"pressure":1010,"humidity":86,"temp_min":277.15,"temp_max":277.15},"wind":{"speed":0.31,"deg":230.5},"rain":{"3h":1},"clouds":{"all":1},"dt":1384797300,"id":5815135,"name":"Washington","cod":200}

这些类是:

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

public class Sys
{
    public double message { get; set; }
    public string country { get; set; }
    public int sunrise { get; set; }
    public int sunset { 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 Main
{
    public double temp { get; set; }
    public double temp_min { get; set; }
    public double temp_max { get; set; }
    public double pressure { get; set; }
    public double sea_level { get; set; }
    public double grnd_level { get; set; }
    public int humidity { get; set; }
}

public class Wind
{
    public double speed { get; set; }
    public double deg { get; set; }
}

public class Clouds
{
    public int all { get; set; }
}

public class RootObject
{
    public Coord coord { get; set; }
    public Sys sys { get; set; }
    public List<Weather> weather { get; set; }
    public string  { get; set; }
    public Main main { get; set; }
    public Wind wind { get; set; }
    public Clouds clouds { get; set; }
    public int dt { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int cod { get; set; }
}

我正在使用 Json.net 从获得的 json 中提取数据。我的 Json 数据没有列表,而是单个对象。我已经看到了如何将列表绑定到 xaml 中的 ListItem 的示例。但我不知道如何在单个对象上做到这一点。 XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="boxOfSearch" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="8*"/>
                <ColumnDefinition Width="2*" />
            </Grid.ColumnDefinitions>
            <TextBox x:Name="getEnteredString" Margin="2,1"/>
            <Button Grid.Column="1" x:Name="btnGet" Tap="btnGet_Tap">Go</Button>
            <!--<Button x:Name="goButton" Content="Go" Grid.Column="1" Margin="2,1"                         Click="goButton_Click"/>-->
        </Grid>

        <Grid Grid.Row="1" x:Name="extractedData" Height="532">
    <Grid.RowDefinitions>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="1*"/>
    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                        <TextBlock x:Name="getName" Text="{Binding name}"  Margin="2,2,2,5" Foreground="#FF176EB5" FontSize="48" TextAlignment="Center" />

我想要输出的是:城市名称、高温和低温。名称和温度来自不同的类。如果我可以分别获得这些值,这对我来说可能是一个很好的起点。

    private void btnGet_Tap(object sender, GestureEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringAsync(new Uri(""));
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

        MessageBox.Show(e.Result);                        

        var rootObj=JsonConvert.DeserializeObject<RootObject>(e.Result);  //Getting DeserializedObject in a variable
        getJsonStrTxtBox.Text = rootObj.name;       // var.name  where name (Name of the City) is a string in RootObject class
    }                                                //like var.name add var.main.temp_max

现在我可以从嵌套的 json 中获取任何数据。非常感谢 Json.net 和 @evanmcdonnal。

【问题讨论】:

    标签: c# json xaml windows-phone-7


    【解决方案1】:

    我对你的问题有点困惑,但听起来你真的只是在寻找这样的东西;

        RootObject response = JsonConvert.DeserializeObject<RootObject>(responseBody);
    
        string message = String.Format("City: {0} - high: {1}, low: {2}", response.name, response.main.temp_max, response.main.temp_mix);
    
         MessageBox.Show(message);
    

    注意:我建议对上面示例中未包含的无效性进行一些检查(response 是否为 null?好的,response.main 是否为 null?)。当您在复杂对象上使用 json.NET 的 DeserializeObject 方法时,反序列化是“级联的”(因为没有更好的术语),这意味着您要反序列化的类所包含的所有对象也将被适当地反序列化。从那里您可以像访问任何其他对象一样访问它们,只是另一个间接层。

    【讨论】:

    • 我已经能够获得城市的名称。使用 object.name 或您提到的 response.name。但是最大和最小温度位于名为“Main”的不同类中,路径是 RootObject-- >主要。
    • @NavS 抱歉,我没有仔细阅读您的类定义,即将更新。
    • 非常感谢。它奏效了。如果没有人指导你,有时你甚至无法得到最简单的东西。
    【解决方案2】:

    使用http://james.newtonking.com/json

     class Program
        {
            static void Main(string[] args)
            {
                var json =
                    "{\"coord\":{\"lon\":-120.5,\"lat\":47.5},\"sys\":{\"message\":0.195,\"country\":\"US\",\"sunrise\":1384787560,\"sunset\":1384820527},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"sky is clear\",\"icon\":\"01d\"}],\"base\":\"gdps stations\",\"main\":{\"temp\":277.15,\"pressure\":1010,\"humidity\":86,\"temp_min\":277.15,\"temp_max\":277.15},\"wind\":{\"speed\":0.31,\"deg\":230.5},\"rain\":{\"3h\":1},\"clouds\":{\"all\":1},\"dt\":1384797300,\"id\":5815135,\"name\":\"Washington\",\"cod\":200}";
    
                dynamic obj = JsonConvert.DeserializeObject(json);
    
                Console.WriteLine(obj.coord.lon + "  " + obj.coord.lat);
    
                Console.ReadLine(); 
            }
        }
    

    首先提取数据并将其映射到类。然后处理 UI 事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2011-03-18
      • 2022-07-12
      • 2020-11-07
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多