调用具有C#关键字的REST API结果JSON可以通过以下步骤完成:
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
using (HttpClient client = new HttpClient())
{
string apiUrl = "https://api.example.com/endpoint";
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
string jsonResult = await response.Content.ReadAsStringAsync();
// 在这里处理JSON结果
}
else
{
Console.WriteLine("请求失败: " + response.StatusCode);
}
}
}
}
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
using (HttpClient client = new HttpClient())
{
string apiUrl = "https://api.example.com/endpoint";
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
string jsonResult = await response.Content.ReadAsStringAsync();
MyModel result = JsonConvert.DeserializeObject<MyModel>(jsonResult);
// 在这里处理解析后的对象
}
else
{
Console.WriteLine("请求失败: " + response.StatusCode);
}
}
}
public class MyModel
{
public string Property1 { get; set; }
public int Property2 { get; set; }
// 添加其他属性
}
}
在上述代码中,MyModel类是一个自定义的数据模型,用于表示JSON中的属性。你可以根据实际情况定义自己的数据模型。
请注意,以上答案仅供参考,具体实现可能因实际情况而异。建议在实际开发中参考相关文档和示例代码,并根据具体需求进行调整和优化。
没有搜到相关的文章