c# - 通过 foreach 循环使用 httpClient.SendAsync(request) 解析 JSON
问题描述
我正在使用 NewtonSoft 解析 JSON Api 调用。
代码必须保持原来的格式。
我唯一的问题是我无法在 foreach 循环中遍历 JSON 的值。
我怎么解决这个问题?
public async Task callWebApi()
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://www.metaweather.com/api/location/search/?lattlong=50.068,-5.316"))
{
var response = await httpClient.SendAsync(request);
using (HttpContent content = response.Content)
{
var jsonString = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<Object>(jsonString);
Console.WriteLine("I need to parse distance, title, location_type, woeid,latt_long so that I can iterate through it using a foreach loop");
Console.WriteLine(data);
Console.Read();
// I don't know how to get the values of the json
}
}
}
解决方案
首先,我认为使用您正在使用的代码拥有指向其他站点的外部链接并不是一个好主意。
话虽如此,您首先需要知道 json 是什么,然后为它创建一个适当的类,在您的情况下,json 看起来像这样
[{"distance":16744,"title":"Penzance","location_type":"City","woeid":31889,"latt_long":"50.11861,-5.53723"},{"distance":19287,"title":"Falmouth","location_type":"City","woeid":19894,"latt_long":"50.151001,-5.07832"},{"distance":19904,"title":"St Ives","location_type":"City","woeid":35662,"latt_long":"50.21032,-5.48569"},{"distance":28619,"title":"Truro","location_type":"City","woeid":38283,"latt_long":"50.263691,-5.054610"},{"distance":90542,"title":"Plymouth","location_type":"City","woeid":32185,"latt_long":"50.375801,-4.136890"},{"distance":146738,"title":"Exeter","location_type":"City","woeid":19792,"latt_long":"50.720760,-3.515340"},{"distance":162575,"title":"Sidmouth","location_type":"City","woeid":34811,"latt_long":"50.687439,-3.23757"},{"distance":197916,"title":"Swansea","location_type":"City","woeid":36758,"latt_long":"51.623150,-3.940930"},{"distance":217189,"title":"Cardiff","location_type":"City","woeid":15127,"latt_long":"51.481251,-3.180730"},{"distance":245712,"title":"Bristol","location_type":"City","woeid":13963,"latt_long":"51.453732,-2.591560"}]
这个类看起来像这样。
public class WeatherClass
{
public int distance { get; set; }
public string title { get; set; }
public string location_type { get; set; }
public int woeid { get; set; }
public string latt_long { get; set; }
}
一旦你有了它,因为你的响应是一个数组对象,当我们反序列化 json 时,我们需要告诉它它是一个数组,所以它知道它一次可以期望多个对象项。
所以需要这样的东西来反序列化响应内容数据。
List<WeatherClass> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<WeatherClass>>(jsonString);
一旦您拥有包含反序列化 json 的数据并将其放入数组中,您就可以随意使用它。
就我而言,这是一个循环的样子。
foreach (var weatherItem in data)
{
Console.WriteLine(weatherItem.distance);
Console.WriteLine(weatherItem.latt_long);
Console.WriteLine(weatherItem.location_type);
Console.WriteLine(weatherItem.title);
Console.WriteLine(weatherItem.woeid);
}
这是您的代码以及我的更改。
public class WeatherClass
{
public int distance { get; set; }
public string title { get; set; }
public string location_type { get; set; }
public int woeid { get; set; }
public string latt_long { get; set; }
}
public async Task callWebApi()
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://www.metaweather.com/api/location/search/?lattlong=50.068,-5.316"))
{
var response = await httpClient.SendAsync(request);
using (HttpContent content = response.Content)
{
var jsonString = await response.Content.ReadAsStringAsync();
List<WeatherClass> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<WeatherClass>>(jsonString);
foreach (var weatherItem in data)
{
Console.WriteLine(weatherItem.distance);
Console.WriteLine(weatherItem.latt_long);
Console.WriteLine(weatherItem.location_type);
Console.WriteLine(weatherItem.title);
Console.WriteLine(weatherItem.woeid);
}
}
}
}
}
static void Main()
{
Program P1 = new Program();
try
{
P1.callWebApi().Wait();
}
catch (Exception ex)
{
Console.WriteLine("There was an exception, {0}", ex.ToString());
Console.Read();
}
}
推荐阅读
- javascript - 如何减小 Kendo.js 文件的大小
- java - 使用 Spring Security 进行身份验证的端点
- python - 如何使用现有的 openssl 库在 linux 中编译 python?
- python - 关于 optuna 中“n_trials”的问题
- c - 获取二叉树高度的算法是如何工作的?
- sql-server - 通过 sp_send_dbmail 中的变量指定 ics 日期
- django - Django登录身份验证:我能够注册新用户并创建新用户,但无法以注册用户身份登录
- openssl - 如何在 CentOS 7 上修复 Certbot 的不完整链?
- php - 如何检查 cmb2 选择字段是否只允许选择唯一值
- reactjs - scalajs-react:如何循环对象的 Seq 以填充 UI