首先获得API,然后用数据测试一次,把获得的json代码在点击打开链接中转换成C#class
变换后的c#代码中会有一个根object(rootObject)
创建http链接
public async static Task<RootObject> GetWeather(string CityName)
{
var myClient = new HttpClient();
string appkey = "86182f21ed1ca8a70d21ff6c5a6eac3f";
string url = string.Format("http://v.juhe.cn/weather/index?cityname={0}&dtype=&format=&key={1}",CityName,appkey);
var response = await myClient.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var data = (RootObject)serializer.ReadObject(ms);
return data;
}
直接调用这个接口获取rootobject就可以了