通过foreach循环使用httpclient.sendasync(request)解析JSON [英] Parse JSON using httpclient.sendasync(request) via a foreach loop
问题描述
我正在使用NewtonSoft来解析JSON Api电话。
代码必须保持原样。
我唯一的问题是我无法在foreach循环中迭代JSON的值。
如何解决这个问题?
public async任务调用WebApi()
{
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);
使用(HttpContent content = response.Content)
{
var jsonString = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject< Object>(jsonString);
Console.WriteLine(我需要解析distance,title,location_type,woeid,latt_long以便我可以使用foreach循环遍历它);
Console.WriteLine(data);
Console.Read();
//我不知道如何获得json的价值
}
}
}
以下是工作代码的链接:
我的尝试:
问题是我不能使用foreach循环迭代。
你需要创建一个代表你的类在C#中使用json对象,然后反序列化。您也可以使用visual studio来获取json,或者您也可以使用 json2csharp.com 。
json的课程是:
public class 位置
{
public < span class =code-keyword> 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 ; }
}
当您收到多个对象即数组时,需要反序列化为上述类型的数组。
var data = JsonConvert.DeserializeObject< Location [] > (jsonString);
现在你可以迭代数组。< br $>
foreach ( var 位置 数据)
{
// 你的逻辑
}
这是关于JSON反序列化的许多常见问题之一,所以我写了一篇文章来回答这些问题:在C#中使用JSON& VB [ ^ ]
am using NewtonSoft to parse a JSON Api call.
The code must remain in the same format it is in.
My only problem is that I cannot iterate through the values of the JSON in a foreach loop.
How can I solve this problem?
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 } } }
Here is a link to the working code:
What I have tried:
The problem is that I cannot iterate using a foreach loop.
you need to create a class that represents your json object in C# and then deserialized to that. You can use visual studio too for getting the json or you can use json2csharp.com for that.
The class for your json is:
public class Location { 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; } }
As you are receiving multiple objects i.e. an array you need to deserialize to an array of the above type.
var data = JsonConvert.DeserializeObject<Location[]>(jsonString);
Now you can iterate on the array.
foreach(var location in data) { // your logic here }
This is one of many common qestions asked on JSON Deserialization, so I have written an article to answer these questions: Working with JSON in C# & VB[^]
这篇关于通过foreach循环使用httpclient.sendasync(request)解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!