如何从OxfordDictionaries api解析此JSON响应? 我已经这样做了,但我收到的关于该对象的数据是空的。
这是我的JSONWord类
public class JSONWord : Connection
{
[JsonProperty("id")]
public string Word { get; set; }
/// <summary>
/// </summary>
[JsonProperty("etymologies")]
public string Etymology { get; set; }
/// <summary>
/// </summary>
[JsonProperty("examples")]
public string Usage { get; set; }
[JsonProperty("definitions")]
public string Defenitions { get; set; } //Todo.
}
这是我解析json数据的代码
HttpWebRequest req = null;
string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/";
string uri = PrimeUrl + "robot";
req = (HttpWebRequest)HttpWebRequest.Create(uri);
//These are not network credentials, just custom headers
req.Headers.Add("app_id", "72536848");
req.Headers.Add("app_key", "0b4ed44eefafe108f88341a4e5cc42ec");
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
WebResponse response = req.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
String responseData = streamReader.ReadToEnd();
JSONWord jsonw = JsonConvert.DeserializeObject<JSONWord>(responseData);
我在responseData变量上获取了json字符串,但没有在JSONWord对象上获取解析数据。可能是什么错误?
EDIT 这是我收到的json字符串
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "robot",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"etymologies": [
"1920s: from Czech, from robota ‘forced labour’. The term was coined in K. Čapek's play R.U.R ‘Rossum's Universal Robots’ (1920)"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"homographNumber": "000",
"senses": [
{
"definitions": [
"(especially in science fiction) a machine resembling a human being and able to replicate certain human movements and functions automatically"
],
"examples": [
{
"text": "a sci-fi movie about time-travelling killer robots"
},
{
"text": "the robot closed the door behind us"
}
],
"id": "m_en_gbus0877780.011",
"subsenses": [
{
"definitions": [
"a machine capable of carrying out a complex series of actions automatically, especially one programmable by a computer"
],
"domains": [
"Electronics"
],
"examples": [
{
"text": "a robot arm"
},
{
"text": "half of all American robots are making cars or trucks"
}
],
"id": "m_en_gbus0877780.008"
},
{
"definitions": [
"a person who behaves in a mechanical or unemotional manner"
],
"examples": [
{
"text": "public servants are not expected to be mindless robots"
}
],
"id": "m_en_gbus0877780.012"
}
]
},
{
"crossReferenceMarkers": [
"another term for crawler"
],
"crossReferences": [
{
"id": "crawler",
"text": "crawler",
"type": "another term for"
}
],
"domains": [
"Computing"
],
"id": "m_en_gbus0877780.014"
},
{
"definitions": [
"a set of automatic traffic lights"
],
"domains": [
"Motoring"
],
"examples": [
{
"text": "waiting at a robot I caught the eye of a young woman"
}
],
"id": "m_en_gbus0877780.016",
"regions": [
"South African"
]
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"pronunciations": [
{
"audioFile": "http://audio.oxforddictionaries.com/en/mp3/robot_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "ˈrəʊbɒt"
}
],
"text": "robot"
}
],
"type": "headword",
"word": "robot"
}
]
}
答案 0 :(得分:2)
您的模型类与json结构不匹配,请尝试使用此模型类(使用https://app.quicktype.io/生成)
public partial class JSONWord
{
[JsonProperty("metadata")]
public Metadata Metadata { get; set; }
[JsonProperty("results")]
public Result[] Results { get; set; }
}
public partial class Metadata
{
[JsonProperty("provider")]
public string Provider { get; set; }
}
public partial class Result
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("lexicalEntries")]
public LexicalEntry[] LexicalEntries { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("word")]
public string Word { get; set; }
}
public partial class LexicalEntry
{
[JsonProperty("entries")]
public Entry[] Entries { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("lexicalCategory")]
public string LexicalCategory { get; set; }
[JsonProperty("pronunciations")]
public Pronunciation[] Pronunciations { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
public partial class Entry
{
[JsonProperty("etymologies")]
public string[] Etymologies { get; set; }
[JsonProperty("grammaticalFeatures")]
public GrammaticalFeature[] GrammaticalFeatures { get; set; }
[JsonProperty("homographNumber")]
public string HomographNumber { get; set; }
[JsonProperty("senses")]
public Sense[] Senses { get; set; }
}
public partial class GrammaticalFeature
{
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class Sense
{
[JsonProperty("definitions")]
public string[] Definitions { get; set; }
[JsonProperty("examples")]
public Example[] Examples { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("subsenses")]
public Subsense[] Subsenses { get; set; }
[JsonProperty("crossReferenceMarkers")]
public string[] CrossReferenceMarkers { get; set; }
[JsonProperty("crossReferences")]
public CrossReference[] CrossReferences { get; set; }
[JsonProperty("domains")]
public string[] Domains { get; set; }
[JsonProperty("regions")]
public string[] Regions { get; set; }
}
public partial class CrossReference
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class Example
{
[JsonProperty("text")]
public string Text { get; set; }
}
public partial class Subsense
{
[JsonProperty("definitions")]
public string[] Definitions { get; set; }
[JsonProperty("domains")]
public string[] Domains { get; set; }
[JsonProperty("examples")]
public Example[] Examples { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
public partial class Pronunciation
{
[JsonProperty("audioFile")]
public string AudioFile { get; set; }
[JsonProperty("dialects")]
public string[] Dialects { get; set; }
[JsonProperty("phoneticNotation")]
public string PhoneticNotation { get; set; }
[JsonProperty("phoneticSpelling")]
public string PhoneticSpelling { get; set; }
}