[英]How i can fetch data from an Api in Json in C#
How i can Fetch data from an Json Api like that: 我如何从这样的Json Api中获取数据:
https://jsonplaceholder.typicode.com/todos/1 https://jsonplaceholder.typicode.com/todos/1
responseBody.title
i Already have this: 我已经有这个:
HttpResponseMessage response = await
client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
You can use Newtonsoft.Json library for working with JSON data. 您可以使用Newtonsoft.Json库来处理JSON数据。 You can use serialization/deserialization or LINQ to JSON methods
您可以使用序列化/反序列化或LINQ to JSON方法
For example based on your code: 例如,基于您的代码:
HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
string responseBody = await response.Content.ReadAsStringAsync();
// LINQ to JSON method
JObject jsonObject = JObject.Parse(responseBody);
int userId = int.Parse(jsonObject["userId"].ToString());
int id = int.Parse(jsonObject["id"].ToString());
string title = jsonObject["title"].ToString();
bool completed = bool.Parse(jsonObject["completed"].ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.