我正在使用 OXFO??RD DICTIONARY API,它賦予單詞意義。它以 JSON 格式回傳結果,因此我只想從 JSON 中提取單詞的含義。我正在使用 C# NEWTONSOFT 包,但無法獲得輸出。非常感謝我在任何幫助下方附加的代碼和輸出螢屏截圖。
using System;
using Newtonsoft.Json;
using System.IO;
using System.Text.Json;
public class Program
{
public static void Main()
{
string word_id = " ";
Console.WriteLine("Enter a word : ");
word_id= Console.ReadLine();
const string lang_code = "en-gb";
const string fields = "definitions";
const string strictMatch = "false";
string WEBSERVICE_URL = "https://od-api.oxforddictionaries.com:443/api/v2/entries/" lang_code '/' word_id "?fields=" fields "&strictMatch=" strictMatch;
try
{
#pragma warning disable SYSLIB0014 // Type or member is obsolete
var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
#pragma warning restore SYSLIB0014 // Type or member is obsolete
if (webRequest != null)
{
webRequest.Method = "GET";
webRequest.Timeout = 12000;
webRequest.ContentType = "application/json";
webRequest.Headers.Add("app_id", "xxxxxxx");
webRequest.Headers.Add("app_key", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
using System.IO.Stream s = webRequest.GetResponse().GetResponseStream();
using System.IO.StreamReader sr = new System.IO.StreamReader(s);
var jsonresponse = sr.ReadToEnd();
var jdata = JsonConvert.DeserializeObject<details>(jsonresponse);
Console.WriteLine("Meaning : ", arg0: jdata.results.lexicalEntries.entries.senses.definitions);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public class details
{
public string id { get; set; }
public Metadata metadata { get; set; }
public results results { get; set; }
public string word { get; set; }
}
public class Metadata
{
public string? operation{ get; set; }
public string? provider { get; set; }
public string? schema{ get; set; }
}
public class results
{
public string? id { get; set; }
public string? language { get; set; }
public lexicalEntries lexicalEntries { get; set; }
public string? type { get; set; }
public string? word { get; set; }
}
public class lexicalEntries
{
public entries entries { get; set; }
}
public class entries
{
public senses senses { get; set; }
}
public class senses
{
public string definitions { get; set; }
public string id { get; set; }
}

uj5u.com熱心網友回復:
我更喜歡使用動態和 ExpandoObject 可以幫助你很多
using System.Dynamic;
dynamic ? obj = JsonConvert.DeserializeObject<ExpandoObject>(yourJSON);
obj 具有與您的 JSON 完全相同的屬性,同時它不需要從任何 JSON 創建模型
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435539.html
