[英]Parse a Json Response from api
我有這個要求
var response = await client.GetAsync("https://api.github.com/[path]");
我應該得到這個回應
{
"name": "",
"path": "",
"sha": "",
"size": ,
"url": "",
"html_url": "",
"git_url": "",
"download_url": "",
"type": "",
"content": "",
"encoding": "",
"_links": {
"self": "",
"git": "",
"html": ""
}
}
我想解析它以獲得“sha”值。
我嘗試了我能找到的一切,所以我無法向你展示我嘗試過的東西。 我希望有人能夠幫助我,ty。
創建一個這樣的類
public class Links
{
public string self { get; set; }
public string git { get; set; }
public string html { get; set; }
}
public class Root
{
public string name { get; set; }
public string path { get; set; }
public string sha { get; set; }
public object size { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string git_url { get; set; }
public string download_url { get; set; }
public string type { get; set; }
public string content { get; set; }
public string encoding { get; set; }
public Links _links { get; set; }
}
在您的項目上安裝Newtonsoft.Json包。
從response.Content
讀取json
作為字符串。
將json
反序列json
Root
對象
var response = await client.GetAsync("https://api.github.com/[path]");
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<Root>(json);
//data.sha
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.