Question

Can someone help me figure out how to get the parent data in JSON data in C# using this sample code?

 string JSON = JsonConvert.SerializeObject(message["data"]);
                //Deserialize to strongly typed class i.e., RootObject
                RootObject obj = JsonConvert.DeserializeObject<RootObject>(JSON);

                //loop through the list and show on console
                foreach (Result resultsItem in obj.results)
                {
                    Console.WriteLine(resultsItem.shipping + "-" + resultsItem.model + "-" + resultsItem.price +
                           "-" + resultsItem.product_name);

                }

and root object class looks like this

public class Result
{
    public string shipping { get; set; }
    public string model { get; set; }
    public string price { get; set; }
    public string item { get; set; }
    public string product_name { get; set; }
    public string availability { get; set; }
}

public class RootObject
{
    public List<object> cookies { get; set; }
    public List<Result> results { get; set; }
    public string connectorGuid { get; set; }
    public string connectorVersionGuid { get; set; }
    public string pageUrl { get; set; }
    public int offset { get; set; }
}

I was able to write code to show column in Result class but I don't know how to reach to "PageUrl" in rootobject class. I tried all possible names in the foreach loop but columns in RootObject are just not accessible. Any help will be greatly appreciated. Thanks

Était-ce utile?

La solution

Is that what you needed?

foreach (Result resultsItem in obj.results)
{
    Console.WriteLine(resultsItem.shipping 
                      + "-" + resultsItem.model 
                      + "-" + resultsItem.price 
                      + "-" + resultsItem.product_name 
                      + "-" + obj.pageUrl);

}

This code will display pageUrl for each line of result.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top