C# 将分层列表转换为数据表/数据集
我有一个JSON:C# 将分层列表转换为数据表/数据集,c#,json.net,strongly-typed-dataset,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": { "
{
"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时,问题就出现了。谁有任何解决方案?我已尝试将其转换为datatable
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;
}
}
公共类YahooImg
{
公共字符串alt{get;set;}
[JsonProperty(“数据文件高度”)]
公共字符串数据文件高度{get;set;}
[JsonProperty(“数据文件宽度”)]
公共字符串数据文件宽度{get;set;}
公共字符串高度{get;set;}
公共字符串src{get;set;}
公共字符串srcset{get;set;}
公共字符串宽度{get;set;}
}
公共级YahooData
{
[JsonProperty(“类别”)]
公共字符串类{get;set;}
公共字符串href{get;set;}
[JsonProperty(“Img”)]
公共yahooimgimg{get;set;}
公共字符串标题{get;set;}
公共字符串内容{get;set;}
公共字符串rel{get;set;}
公共列表span{get;set;}
公共列表wbr{get;set;}
}
公开课成绩
{
[JsonProperty(“A”)]
公共列表a{get;set;}
}
公共类YahooQuery
{
公共整数计数{get;set;}
已创建公共字符串{get;set;}
公共字符串lang{get;set;}
[JsonProperty(“结果”)]
公共yahoo结果{get;set;}
}
公共类YahooAPIData
{
[JsonProperty(“查询”)]
公共YahooQuery YahooQuery{get;set;}
公共YahooQuery GetYahooResult(字符串url)
{
var ds=新的YahooAPIData();
如果(!url.ToLower().包含(“yahoo”))
返回ds.Yahooquery;
尝试
{
var wc=新的WebClient();
Add(HttpRequestHeader.Accept,“application/json”);
var response=wc.DownloadString(url);
var dss=JsonConvert.DeserializeObject(响应);
}
捕获(异常扩展)
{
var r=exp;
}
返回ds.Yahooquery;
}
}
我不确定您到底想做什么,因为您还没有发布要解析的数据集结构,但是,如果您创建以下类,我认为您可以在查询对象模型中解析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;
}
请不要链接到外部网站,除非它是为了支持您问题中已经存在的内容。您确实需要删除
捕获(异常exp)
-捕获这样的内容是不好的做法。您还需要处理WebClient
。您至少可以演示如何将JSON转换为此对象模型吗?顺便说一句,现在还没有编译。@Trikaldarshi我有和你提到的相同的类结构。我将json转换为列表问题是当我将结果列表转换为数据表时,因为列表具有层次数据。@Enigmativity:有几个库可用于将json转换为对象模型viz。Json,这取决于你想要使用什么。我已经试着做了转换。我做不到。这个答案并没有以当前的形式解决这个问题。答案的第一行清楚地提到了这一点,它只提供了一个可以解析json字符串的C#对象模型。