我正在尝试使用HttpClient调用API,并将json反序列化为响应对象。在此json中,有一个琐事问题对象列表。当我将对象设置为反序列化对象时,列表保持为空。
我检查了HttpClient是否可以正常工作,我也尝试过使用JsonConvert。
这些是TriviaQuestion和Response类:
public class TriviaQuestion
{
public string Category { get; set; }
public string Type { get; set; }
public string Difficulty { get; set; }
public string Question { get; set; }
public string CorrectAnswer { get; set; }
public List<string> IncorrectAnswers { get; set; }
public override string ToString()
{
return $"Question: {Question}";
}
}
public class Response
{
public int ResponseCode { get; set; }
public List<TriviaQuestion> Questions { get; set; }
public Response()
{
Questions = new List<TriviaQuestion>();
}
}
这是反序列化的代码
private static HttpClient client = new HttpClient();
private static string URL = "https://opentdb.com/api.php";
private static string urlParameters = "?amount=1";
static void Main()
{
RunAsync().GetAwaiter().GetResult();
}
static async Task RunAsync()
{
client.BaseAddress = new Uri(URL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
Response response = new Response();
try
{
response = await GetResponseAsync(urlParameters);
ShowResponse(response);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
static async Task<Response> GetResponseAsync(string path)
{
Response response = new Response();
//string responseString = "";
HttpResponseMessage httpResponse = await client.GetAsync(path);
if (httpResponse.IsSuccessStatusCode)
{
//responseString = httpResponse.Content.ReadAsStringAsync().Result;
response = await httpResponse.Content.ReadAsAsync<Response>();
}
//response = JsonConvert.DeserializeObject<Response>(responseString);
return response;
}
我希望得到一个琐事问题对象的列表,但是该列表保持计数=0。如果我打印出jsonString,我得到的是结果:
{
"response_code":0,
"results": [
{
"category":"Entertainment: Video Games",
"type":"multiple",
"difficulty":"medium",
"question":"In Need for Speed: Underground, what car does Eddie drive?",
"correct_answer":"Nissan Skyline GT-R (R34)",
"incorrect_answers": [
"Mazda RX-7 FD3S",
"Acura Integra Type R",
"Subaru Impreza 2.5 RS"
]
}]
}
感谢您的帮助!
答案 0 :(得分:1)
您的Response
类有点错误。它与您发布的JSON不匹配。
public List<TriviaQuestion> Questions { get; set; }
应为:
public List<TriviaQuestion> Results { get; set; }
此外,由于您的JSON具有蛇形外壳,因此要捕获response_code
,correct_answer
和incorrect_answers
值,您将需要用JsonProperty
属性修饰类属性,即[JsonProperty(PropertyName = "incorrect_answers")]
,也可以使用ContractResolver:
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
var response = JsonConvert.DeserializeObject<Response>(json, new JsonSerializerSettings
{
ContractResolver = contractResolver
});
因此,您的全部课程将是:
public class TriviaQuestion
{
public string Category { get; set; }
public string Type { get; set; }
public string Difficulty { get; set; }
public string Question { get; set; }
// only need this if not using the ContractResolver
[JsonProperty(PropertyName = "correct_answer")]
public string CorrectAnswer { get; set; }
// only need this if not using the ContractResolver
[JsonProperty(PropertyName = "incorrect_answers")]
public List<string> IncorrectAnswers { get; set; }
}
public class Response
{
// only need this if not using the ContractResolver
[JsonProperty(PropertyName = "response_code")]
public int ResponseCode { get; set; }
public List<TriviaQuestion> Results { get; set; }
}
然后您将可以反序列化:
var json = "{\r\n \"response_code\":0,\r\n \"results\": [\r\n { \r\n \"category\":\"Entertainment: Video Games\",\r\n \"type\":\"multiple\",\r\n \"difficulty\":\"medium\",\r\n \"question\":\"In Need for Speed: Underground, what car does Eddie drive?\",\r\n \"correct_answer\":\"Nissan Skyline GT-R (R34)\",\r\n \"incorrect_answers\": [\r\n \"Mazda RX-7 FD3S\",\r\n \"Acura Integra Type R\",\r\n \"Subaru Impreza 2.5 RS\"\r\n ]\r\n }]\r\n}";
// if using JsonProperty attributes
var response = JsonConvert.DeserializeObject<Response>(json);
// or
// if using ContractResolver
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
var response = JsonConvert.DeserializeObject<Response>(json, new JsonSerializerSettings
{
ContractResolver = contractResolver
});
答案 1 :(得分:0)
将返回的JSON转换为响应:
var json = await httpResponse.Content.ReadAsStringAsync();
response= JsonConvert.DeserializeObject<Response>(json);