JSON parse and retrieve string

2.3k Views Asked by At

I am trying to get the element to print the browser_download_url from the JSON of https://api.github.com/repos/MyBotRun/MyBot/releases/latest

It will not print the browser_download_url

public class Asset
{
 public string browser_download_url { get; set; }
}


wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36Accept");             
var json = wc.DownloadString(@"https://api.github.com/repos/MyBotRun/MyBot/releases/latest");
if (json.Contains("browser_download_url"))
{
  Asset asset = JsonConvert.DeserializeObject<Asset>(json);
  Console.WriteLine(asset.browser_download_url);
}
1

There are 1 best solutions below

5
Minijack On BEST ANSWER

The JSON in the URL that you link does not match the Asset class that you are trying to deserialize to.

I would recommend using a tool like quicktype.io and paste your JSON in there and it will generate the C# classes for you.

After copying that example class you could then use the following code:

var asset = Asset.FromJson(json);

Console.WriteLine(asset.AssetElement[0].BrowserDownloadUrl);