簡體   English   中英

解析來自 api 的 Json 響應

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM