【发布时间】:2018-08-27 15:12:06
【问题描述】:
如何解析来自 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 对象上解析的数据。可能是什么错误?
编辑 这是我收到的 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"
}
]
}
【问题讨论】:
-
你应该分享来自 responsedata 的 json 字符串,这样每个人都可以轻松使用
-
分享了响应数据