首先你需要創建類來反序列化你的json。爲此,您可以使用VS的編輯 - >選擇性粘貼 - >粘貼JSON作爲類或使用一個網站就像JsonUtils:
public class JokeInfo
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("joke")]
public string Joke { get; set; }
[JsonProperty("categories")]
public IList<string> Categories { get; set; }
}
public class ServerResponse
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public JokeInfo JokeInfo { get; set; }
}
然後使用庫像JSON.NET反序列化數據:
// jokeJsonString is the response you get from the server
var serverResponse = JsonConvert.DeserializeObject<ServerResponse>(jokeJsonString);
// Then you can access the content like this:
var theJoke = serverResponse.JokeInfo.Joke;
你可以只是使用佔位符而不是真正的笑話,因爲你在這樣的問答網絡上發佈代碼...... –
@FᴀʀʜᴀɴAɴᴀᴍ你在編輯這個笑話時是正確的。海報鏈接到一個笑話api,返回隨機笑話 – Nkosi