原谅我缺乏知识的人,尽管我前一段时间对C#有点涉猎,但我是一名数据库专家。我试图弄清楚如何使此API运行。
我要使用的API来自{{3}}。几乎没有任何文档可以指导我。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using unirest_net.http;
using unirest_net;
namespace NBA_test
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Start ...");
Task<HttpResponse<MyClass.RootObject>> response = Unirest.get("https://api-nba-v1.p.rapidapi.com/gameDetails/5162")
.header("X-RapidAPI-Host", "api-nba-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "myKey")
.asJsonAsync<MyClass.RootObject>();
var status = response.Status;
Console.WriteLine("End ....");
}
}
public class MyClass
{
public class Result
{
public string seasonYear { get; set; }
public int gameId { get; set; }
public string arena { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
}
}
var状态从“已创建”变为“正在运行”,就这样,程序关闭。没有错误消息,但我不知道如何从该API中获取JSON。我知道我想念什么,但不知道。
答案 0 :(得分:1)
您正在使用sync
主要方法在控制台应用程序中。您不应该在async
方法内调用sync
方法。我将您的async
通话变成了sync
通话:
public static void Main(string[] args)
{
Console.WriteLine("Start ...");
var response = Unirest.get("https://api-nba-v1.p.rapidapi.com/gameDetails/5162")
.header("X-RapidAPI-Host", "api-nba-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "myKey")
.asJson<RootObject>();
var status = response.Status;
Console.WriteLine("End ....");
}
您仍然可能会问反序列化的JSON
在哪里?
根据Unirest docs:
回复 收到响应后,Unirest会在 对象的形式,此对象应始终具有相同的键 有关回复详细信息的每种语言。
.Code-HTTP响应状态代码(示例200) .Headers-HTTP 响应标题 .Body-解析的响应正文(如果适用) JSON响应示例将解析为对象/关联数组。 .Raw-未解析的响应正文
基本上,您可以像这样访问结果:
if (response.Code == 200) // Success, OK in HTTP Codes
{
response.Body; // which body has the type of MyClass.RootObject
}
完整的示例:
public static void Main(string[] args)
{
Console.WriteLine("Start ...");
var response = Unirest.get("https://api-nba-v1.p.rapidapi.com/gameDetails/5162")
.header("X-RapidAPI-Host", "api-nba-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "myKey")
.asJson<RootObject>();
if (response.Code == 200) // Success, OK in HTTP Codes
{
response.Body; // which body has the type of MyClass.RootObject
}
Console.WriteLine("End ....");
Console.ReadLine(); // to force command line stay up for an input and not close applicaiton immediately aftter runing it.
}
更新1:
这是Unirest在.NET Fiddle上的现场直播和工作:
答案 1 :(得分:0)
正如其他人指出的那样,您需要添加一些等待语句。我对api进行了许多调用,这些调用花费了一些时间在我的一个应用程序中进行处理。根据其他评论,我进行了更新,以允许您在等待调用返回时执行逻辑。您对代码的修改是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using unirest_net.http;
using unirest_net;
namespace NBA_test
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Start ...");
//the added Wait() causes the thread to hold until the task is finished.
Task<HttpResponse<MyClass.RootObject>> response = Unirest.get("https://api-nba-v1.p.rapidapi.com/gameDetails/5162")
.header("X-RapidAPI-Host", "api-nba-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "myKey")
.asJsonAsync<MyClass.RootObject>();
//if need to perform other logic while you are waiting
while(response.Status == TaskStatus.Running)
{
// perform other logic like gui here
}
var status = response.Status;
Console.WriteLine("End ....");
}
}
public class MyClass
{
public class Result
{
public string seasonYear { get; set; }
public int gameId { get; set; }
public string arena { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
}
}
答案 2 :(得分:0)
您可以使用httpClient
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
var response = httpClient.GetStringAsync(new Uri(url)).Result;
var releases = JArray.Parse(response);
}
答案 3 :(得分:0)
原来,该API的文档存在缺陷。我设法通过简单地使用字符串(和Newtonsoft的Json.NET)使其工作。谢谢@AliBahrami。代码现在看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using unirest_net.http;
using unirest_net;
using Newtonsoft.Json.Linq;
namespace NBA_test
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Start of Program...");
HttpResponse<string> response = Unirest.get("https://api-nba-v1.p.rapidapi.com/gameDetails/9999")
.header("X-RapidAPI-Host", "api-nba-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "myKey")
.asJson<string>();
var result = response.Body;
JObject parsedString = JObject.Parse(result);
RootObject myGame = parsedString.ToObject<RootObject>();
// Get game id
Console.WriteLine(myGame.results[0].gameId);
Console.WriteLine("End of Program....");
}
}
}