在c#中从嵌套的Json中提取数据

时间:2013-11-18 18:53:49

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

我想为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非常感谢你。

City name plus temprature

2 个答案:

答案 0 :(得分: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.main null吗?)。当你在一个复杂的对象上使用json.NET的DeserializeObject方法时,反序列化就是“级联”(缺少更好的术语),这意味着你要反序列化的类的所有对象也将被适当地反序列化。从那里你可以像任何其他对象一样访问它们,只有另一层间接。

答案 1 :(得分:1)

使用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事件。