Ummiaa
Ummiaa

Reputation: 5

JSON parse and retrieve string

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);
}

Upvotes: 0

Views: 2360

Answers (1)

NO TO OPENAI
NO TO OPENAI

Reputation: 685

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);

Upvotes: 2

Related Questions