【发布时间】: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