将分层列表转换为Datatable / dataSet

时间:2016-03-12 09:27:34

标签: c# json.net strongly-typed-dataset

我有这个JSON:

{
  "query": {
    "count": 43,
    "created": "2016-03-12T09:46:36Z",
    "lang": "en-US",
    "results": {
      "a": [
        {
          "class": "image",
          "href": "/wiki/File:Yahoo!_logo.svg",
          "img": {
            "alt": "Yahoo! logo.svg",
            "data-file-height": "233",
            "data-file-width": "1000",
            "height": "51",
            "src": "//upload.wikimedia.org/wikipedia/commons/thumb/2/24/
                    Yahoo%21_logo.svg/220px-Yahoo%21_logo.svg.png",
            "srcset": "//upload.wikimedia.org/wikipedia/commons/thumb/2/24/
                       Yahoo%21_logo.svg/330px-Yahoo%21_logo.svg.png 1.5x
                      , //upload.wikimedia.org/wikipedia/commons/thumb/2/24/
                      Yahoo%21_logo.svg/440px-Yahoo%21_logo.svg.png 2x",
            "width": "220"
          }
        },
        {
          "href": "/wiki/Types_of_business_entity",
          "title": "Types of business entity",
          "content": "Type"
        },
        {
          "href": "/wiki/Public_company",
          "title": "Public company",
          "content": "Public"
        },
        {
          "href": "/wiki/Ticker_symbol",
          "title": "Ticker symbol",
          "content": "Traded as"
        },
        {
          "href": "/wiki/NASDAQ",
          "title": "NASDAQ",
          "content": "NASDAQ"
        },
        {
          "class": "external text",
          "href": "http://www.nasdaq.com/symbol/yhoo",
          "rel": "nofollow",
          "content": "YHOO"
        },
        {
          "href": "/wiki/NASDAQ-100",
          "title": "NASDAQ-100",
          "content": "NASDAQ-100 Component"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/S%26P_500",
          "title": "S&P 500",
          "content": "S&P 500 Component"
        },
        {
          "href": "/wiki/Sunnyvale,_California",
          "title": "Sunnyvale, California",
          "content": "Sunnyvale"
        },
        {
          "href": "/wiki/Entrepreneurship",
          "title": "Entrepreneurship",
          "content": "Founder(s)"
        },
        {
          "href": "/wiki/Jerry_Yang_(entrepreneur)",
          "title": "Jerry Yang (entrepreneur)",
          "content": "Jerry Yang"
        },
        {
          "href": "/wiki/David_Filo",
          "title": "David Filo",
          "content": "David Filo"
        },
        {
          "href": "/wiki/Maynard_Webb",
          "title": "Maynard Webb",
          "content": "Maynard Webb"
        },
        {
          "href": "/wiki/Marissa_Mayer",
          "title": "Marissa Mayer",
          "content": "Marissa Mayer"
        },
        {
          "href": "/wiki/David_Filo",
          "title": "David Filo",
          "content": "David Filo"
        },
        {
          "href": "#cite_note-1",
          "span": [
            "[",
            "]"
          ],
          "content": "1"
        },
        {
          "href": "/wiki/Product_(business)",
          "title": "Product (business)",
          "content": "Products"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/Yahoo_News",
          "title": "Yahoo News",
          "content": "Yahoo News"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/Yahoo_Mail",
          "title": "Yahoo Mail",
          "content": "Yahoo Mail"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/Yahoo_Finance",
          "title": "Yahoo Finance",
          "content": "Yahoo Finance"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/Yahoo_Sports",
          "title": "Yahoo Sports",
          "content": "Yahoo Sports"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/Yahoo_Search",
          "title": "Yahoo Search",
          "content": "Yahoo Search"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/Yahoo_Messenger",
          "title": "Yahoo Messenger",
          "content": "Yahoo Messenger"
        },
        {
          "href": "/wiki/Yahoo!_Answers",
          "title": "Yahoo! Answers",
          "content": "Yahoo! Answers"
        },
        {
          "href": "/wiki/Tumblr",
          "title": "Tumblr",
          "content": "Tumblr"
        },
        {
          "href": "/wiki/Flickr",
          "title": "Flickr",
          "content": "Flickr"
        },
        {
          "href": "/wiki/List_of_Yahoo!-owned_sites_and_services",
          "title": "List of Yahoo!-owned sites and services",
          "content": "See Yahoo products"
        },
        {
          "href": "/wiki/Revenue",
          "title": "Revenue",
          "content": "Revenue"
        },
        {
          "href": "#cite_note-2",
          "span": [
            "[",
            "]"
          ],
          "content": "2"
        },
        {
          "href": "/wiki/Earnings_before_interest_and_taxes",
          "title": "Earnings before interest and taxes",
          "content": "Operating income"
        },
        {
          "href": "#cite_note-10K-3",
          "span": [
            "[",
            "]"
          ],
          "content": "3"
        },
        {
          "href": "/wiki/Net_income",
          "title": "Net income",
          "content": "Net income"
        },
        {
          "href": "#cite_note-4",
          "span": [
            "[",
            "]"
          ],
          "content": "4"
        },
        {
          "href": "/wiki/Asset",
          "title": "Asset",
          "content": "Total assets"
        },
        {
          "href": "#cite_note-5",
          "span": [
            "[",
            "]"
          ],
          "content": "5"
        },
        {
          "href": "/wiki/Equity_(finance)",
          "title": "Equity (finance)",
          "content": "Total equity"
        },
        {
          "href": "#cite_note-6",
          "span": [
            "[",
            "]"
          ],
          "content": "6"
        },
        {
          "href": "#cite_note-7",
          "span": [
            "[",
            "]"
          ],
          "content": "7"
        },
        {
          "class": "mw-redirect",
          "href": "/wiki/List_of_acquisitions_by_Yahoo!",
          "title": "List of acquisitions by Yahoo!",
          "content": "Yahoo subsidiaries"
        },
        {
          "class": "external text",
          "href": "https://www.yahoo.com",
          "rel": "nofollow",
          "wbr": [
            null,
            null
          ],
          "content": "www.yahoo.com"
        },
        {
          "href": "/wiki/Alexa_Internet",
          "title": "Alexa Internet",
          "content": "Alexa"
        },
        {
          "class": "external text",
          "href": "//en.wikipedia.org/w/index.php?title=Yahoo!&action=edit",
          "content": "[update]"
        },
        {
          "href": "#cite_note-alexaranking-8",
          "span": [
            "[",
            "]"
          ],
          "content": "8"
        }
      ]
    }
  }
}

...但我无法将其转换为DataTable

中的.Net

我需要将回复转换为DataSet

以下是我在转换response时使用的类。我知道我们可以轻松转换列表中的响应但是当我们将响应转换为数据表或数据集时,会出现问题吗?我试图将其转换为数据表

public class YahooImg
{
    public string alt { get; set; }
    [JsonProperty("data-file-height")]
    public string data_file_height { get; set; }
    [JsonProperty("data-file-width")]
    public string data_file_width { get; set; }
    public string height { get; set; }
    public string src { get; set; }
    public string srcset { get; set; }
    public string width { get; set; }
}
public class YahooData
{
    [JsonProperty("class")]
    public string CLASS { get; set; }
    public string href { get; set; }
    [JsonProperty("Img")]
    public YahooImg img { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public string rel { get; set; }
    public List<string> span { get; set; }
    public List<object> wbr { get; set; }
}
public class YahooResults
{
    [JsonProperty("A")]
    public List<YahooData> a { get; set; }
}
public class YahooQuery
{
    public int count { get; set; }
    public string created { get; set; }
    public string lang { get; set; }
    [JsonProperty("Results")]
    public YahooResults results { get; set; }
}
public class YahooAPIData
{
    [JsonProperty("query")]
    public YahooQuery Yahooquery { get; set; }
    public YahooQuery GetYahooResult(string url)
    {
        var ds = new YahooAPIData();
        if (!url.ToLower().Contains("yahoo"))
            return ds.Yahooquery;
        try
        {
            var wc = new WebClient();
            wc.Headers.Add(HttpRequestHeader.Accept, "application/json");
            var response = wc.DownloadString(url);
           var dss = JsonConvert.DeserializeObject<YahooAPIData>(response);               
        }
        catch (Exception exp)
        {
            var r = exp;
        }
        return ds.Yahooquery;
    }
}  

1 个答案:

答案 0 :(得分:-1)

我不确定你想要做什么,因为你还没有发布和要解析的数据集结构,但是,如果你创建以下类,那么我认为你可以在Query对象模型中解析你的JSON内容。 / p>

public class Query
{
    int count;
    datetime created;
    string lang;
    Result[] a; 
}

public class Result
{

    string _class;
    string href;
    Image img;

}

public class Image
{

    string alt;
    string data_file_height;
    string data_file_width;
    string height;
    string src;
    string srcset;
    string width;

}