Search code examples
c#jsonjson.netscreen-scraping

C# JSON Get specific attribute with multiple roots


So basically, I am building a C# application that will retrieve specific values from a URL. The URL is URL to Scrape

The attributes that I need to grab are: 'id', 'size', 'inStock' and 'ATS' from each different size of product.

Example

I have tried the code from this URL but no luck.

string data = client.DownloadString(region).Replace("\n", "").Replace("\r", "").Replace(@"\", "").Replace("\t", "");
dynamic dynObj = JsonConvert.DeserializeObject(data);
var someVar1 = dynObj["variants"]["BY1910_530"][1]["id"].ToString();
MessageBox.Show(someVar1);

Solution

  • Please try something like this (As you are using JsonConvert, I assume you've already got the Newtonsoft.Json Nuget package):

    var client = new HttpClient();
    string json = await client.GetStringAsync(@"http://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/uk_UK/Product-GetVariants?pid=BY191");
    var jobj = JObject.Parse(json);
    Console.WriteLine(jobj["variations"]["variants"][1]["id"].ToString());