我是一个新手,所以请原谅任何不规范的问题程序!
基本上,我正在尝试从Pearson Dictionary Web API反序列化一个json数组。以下是JSON(我删除了一些results
索引以节省空间):
{
"status": 200,
"offset": 0,
"limit": 10,
"count": 10,
"total": 47,
"url": "/v2/dictionaries/ldoce5/entries?headword=test",
"results": [
{
"datasets": [
"ldoce5",
"dictionary"
],
"headword": "test",
"homnum": 1,
"id": "cqAFzDfHTM",
"part_of_speech": "noun",
"pronunciations": [
{
"audio": [
{
"lang": "British English",
"type": "pronunciation",
"url": "/v2/dictionaries/assets/ldoce/gb_pron/brelasdetest.mp3"
},
{
"lang": "American English",
"type": "pronunciation",
"url": "/v2/dictionaries/assets/ldoce/us_pron/test1.mp3"
}
],
"ipa": "test"
}
],
"senses": [
{
"definition": [
"a set of questions, exercises, or practical activities to measure someone's skill, ability, or knowledge"
],
"examples": [
{
"audio": [
{
"type": "example",
"url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001626298.mp3"
}
],
"text": "Did you get a good mark in the test ?"
}
],
"gramatical_examples": [
{
"examples": [
{
"audio": [
{
"type": "example",
"url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-000592041.mp3"
}
],
"text": "We have a test on irregular verbs tomorrow."
}
],
"pattern": "test on"
}
],
"signpost": "exam"
}
],
"url": "/v2/dictionaries/entries/cqAFzDfHTM"
}
]
}
以下是我使用的 C# 代码来反序列化上述内容:
class Program
{
static void Main(string[] args)
{
string word = "test";
string sURL = "https://api.pearson.com:443/v2/dictionaries/ldoce5/entries?headword=" + word;
WebClient client = new WebClient();
string full = client.DownloadString(sURL);
var final = JsonConvert.DeserializeObject<Dictionary>(full);
Console.WriteLine(final.results[0].senses.definition);
}
}
public class Dictionary
{
public Result[] results { get; set; }
}
public class Result
{
public string part_of_speech { get; set; }
public Senses senses { get; set; }
}
public class Senses
{
public string definition { get; set; }
}
出现了奇怪的错误,如下所示:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'TestingJson.Senses',因为该类型需要一个 JSON 对象(例如 {"name":"value"})才能正确反序列化。要解决此错误,请将 JSON 更改为 JSON 对象(例如{"name":"value"}),或将反序列化类型更改为数组或实现集合接口(例如 ICollection、IList)的类型,例如可以从 JSON 数组反序列化的 List。也可以向类型添加 JsonArrayAttribute,以强制从 JSON 数组反序列化。路径'results[0].senses',行 1,位置 512。
希望能得到帮助!