【问题标题】:Convert Hierarchical List into Datatable/dataSet将分层列表转换为数据表/数据集
【发布时间】:2016-03-12 09:27:34
【问题描述】:

我有这个 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"
        }
      ]
    }
  }
}

...但我无法在.Net 中将其转换为DataTable

我需要将响应转换为DataSet

以下是我在转换响应时使用的类。我知道我们很容易将响应转换为列表,但是当我们将响应转换为数据表或数据集时出现问题,任何人有任何解决方案吗?我试图将其转换为数据表

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

【问题讨论】:

  • 请不要链接到外部网站,除非它是为了支持您问题中已有的内容。
  • 您确实需要删除catch (Exception exp) - 捕获这样的所有内容是一种不好的做法。您还需要处理WebClient

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


【解决方案1】:

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

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;

}

【讨论】:

  • 你能至少展示一下如何将 JSON 转换为这个对象模型吗?附:这目前甚至无法编译。
  • @Trikaldarshi 我有你提到的相同的类结构。我将json转换为列表问题是当我将结果列表转换为数据表时,因为列表具有分层数据。
  • @Enigmativity:有几个库可用于将 JSON 转换为对象模型,即。 Newtonsoft.Json,由你决定使用。
  • 我已尝试进行转换。我不能让它工作。这个答案没有解决当前形式的问题。
  • 答案的第一行明确提到了这一点,它只有助于提供一个可以解析这个 json 字符串的 C# 对象模型。
猜你喜欢
  • 2015-04-14
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多