0

有人可以帮我弄清楚如何使用此示例代码在 C# 中获取 JSON 数据中的父数据吗?

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

                }

和根对象类看起来像这样

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

我能够编写代码来显示 Result 类中的列,但我不知道如何访问 rootobject 类中的“PageUrl”。我在 foreach 循环中尝试了所有可能的名称,但 RootObject 中的列无法访问。任何帮助将不胜感激。谢谢

4

1 回答 1

1

那是你需要的吗?

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

}

此代码将pageUrl针对每一行结果显示。

于 2014-01-06T00:06:24.513 回答