将json字符串转换为数据集c#的问题?

时间:2017-06-24 13:53:24

标签: c# json

我正在尝试使用JSON.Net库将json字符串转换为数据集。但是在完成反序列化对象后,我不断收到错误'在JSON字符串中找到的附加文本。'我能做错什么?

JSON字符串 -

[
   {
      "title":"'Grenfell changes everything': Shadow of high-rise fire looms across UK",
      "pubDate":"2017-06-24 10:55:47",
      "link":"http://www.cnn.com/2017/06/24/europe/london-fire-grenfell-tower/index.html",
      "guid":"http://www.cnn.com/2017/06/24/europe/london-fire-grenfell-tower/index.html",
      "author":"",
      "thumbnail":"",
      "description":"Ten days after an inferno swept through Grenfell Tower in west London, leaving the apartment block a blackened shell and claiming the lives of at least 79 people, a shadow hangs over Britain.",
      "content":"Ten days after an inferno swept through Grenfell Tower in west London, leaving the apartment block a blackened shell and claiming the lives of at least 79 people, a shadow hangs over Britain.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170621150157-01-london-protests-0621-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Saudis: Attack foiled on Grand Mosque in Mecca",
      "pubDate":"2017-06-24 10:27:00",
      "link":"http://www.cnn.com/2017/06/23/middleeast/grand-mosque-attack-foiled/index.html",
      "guid":"http://www.cnn.com/2017/06/23/middleeast/grand-mosque-attack-foiled/index.html",
      "author":"",
      "thumbnail":"",
      "description":"Saudi security forces on Friday prevented an imminent attack on the Grand Mosque in Mecca, the country's Interior Ministry said on state-run TV.",
      "content":"Saudi security forces on Friday prevented an imminent attack on the Grand Mosque in Mecca, the country's Interior Ministry said on state-run TV.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170624130705-mecca-plot-foiled-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Qatar handed stern list of 13 demands by Arab states",
      "pubDate":"2017-06-24 09:24:20",
      "link":"http://www.cnn.com/2017/06/23/middleeast/gulf-nations-qatar-demands/index.html",
      "guid":"http://www.cnn.com/2017/06/23/middleeast/gulf-nations-qatar-demands/index.html",
      "author":"",
      "thumbnail":"",
      "description":"Four Arab states that have isolated Qatar have handed the country a list of 13 demands, including some that are likely to infuriate Doha and exacerbate the region's worst crisis in decades.",
      "content":"Four Arab states that have isolated Qatar have handed the country a list of 13 demands, including some that are likely to infuriate Doha and exacerbate the region's worst crisis in decades.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/160619161356-al-jazeera-hq-qatar-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Turkey to stop teaching evolution in high schools",
      "pubDate":"2017-06-24 06:21:59",
      "link":"http://www.cnn.com/collections/intl-turkey-evolution-theory/",
      "guid":"http://www.cnn.com/collections/intl-turkey-evolution-theory/",
      "author":"",
      "thumbnail":"",
      "description":"",
      "content":"",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170613133545-01-recep-tayyip-erdogan-0613-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"China: More than 100 feared buried in landslide",
      "pubDate":"2017-06-24 10:03:43",
      "link":"http://www.cnn.com/2017/06/23/china/china-landslide/index.html",
      "guid":"http://www.cnn.com/2017/06/23/china/china-landslide/index.html",
      "author":"",
      "thumbnail":"",
      "description":"A landslide in southwest China's Sichuan province buried more than 100 people, the Chinese state-run broadcaster China Central Television (CCTV) reported Saturday.",
      "content":"A landslide in southwest China's Sichuan province buried more than 100 people, the Chinese state-run broadcaster China Central Television (CCTV) reported Saturday.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170624160125-xinmo-landslide-distance-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Ex-Obama official: Handling of Russia 'is hardest thing to defend,' WaPo reports",
      "pubDate":"2017-06-23 17:47:54",
      "link":"http://www.cnn.com/collections/intl-wapo-former-obama-official/",
      "guid":"http://www.cnn.com/collections/intl-wapo-former-obama-official/",
      "author":"",
      "thumbnail":"",
      "description":"",
      "content":"",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170615132528-01-vladimir-putin-0615-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"White House banned cameras, so CNN sent a sketch artist",
      "pubDate":"2017-06-24 01:42:28",
      "link":"http://money.cnn.com/2017/06/23/media/white-house-press-briefing-sketch-artist/index.html",
      "guid":"http://money.cnn.com/2017/06/23/media/white-house-press-briefing-sketch-artist/index.html",
      "author":"",
      "thumbnail":"",
      "description":"",
      "content":"",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623172331-wh-briefing-colored-sketches-1-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Prosecutors ask to reinstate Hernandez's murder conviction",
      "pubDate":"2017-06-23 21:37:38",
      "link":"http://www.cnn.com/2017/06/23/us/aaron-hernandez-appeal-conviction/index.html",
      "guid":"http://www.cnn.com/2017/06/23/us/aaron-hernandez-appeal-conviction/index.html",
      "author":"",
      "thumbnail":"",
      "description":"Bristol County prosecutors officially asked to reinstate Aaron Hernandez's murder conviction on Friday, arguing in court documents that the former NFL player's suicide should not lead to dismissal of the guilty verdict.",
      "content":"Bristol County prosecutors officially asked to reinstate Aaron Hernandez's murder conviction on Friday, arguing in court documents that the former NFL player's suicide should not lead to dismissal of the guilty verdict.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170420132012-01-aaron-hernandez-court-file-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"White House responds to Comey tape inquiry with Trump tweet",
      "pubDate":"2017-06-24 10:29:09",
      "link":"http://www.cnn.com/2017/06/23/politics/white-house-comey-tapes-trump-tweet/index.html",
      "guid":"http://www.cnn.com/2017/06/23/politics/white-house-comey-tapes-trump-tweet/index.html",
      "author":"",
      "thumbnail":"",
      "description":"The White House is letting President Donald Trump's tweet Thursday -- asserting that he had not taped former FBI Director James Comey -- stand as the President's official statement.",
      "content":"The White House is letting President Donald Trump's tweet Thursday -- asserting that he had not taped former FBI Director James Comey -- stand as the President's official statement.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623093958-donald-trump-0622-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Trump faces Putin in 2 weeks",
      "pubDate":"2017-06-23 19:41:29",
      "link":"http://www.cnn.com/2017/06/23/politics/trump-putin-meeting-g20/index.html",
      "guid":"http://www.cnn.com/2017/06/23/politics/trump-putin-meeting-g20/index.html",
      "author":"",
      "thumbnail":"",
      "description":"Inside the capacious Hamburg Messe conference hall next month, President Donald Trump will come face-to-face with the man whose shadow has darkened much of his presidency: Vladimir Putin.",
      "content":"Inside the capacious Hamburg Messe conference hall next month, President Donald Trump will come face-to-face with the man whose shadow has darkened much of his presidency: Vladimir Putin.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170407053321-02-trump-putin-split-0407-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"New Zealand's All Blacks overpower Lions in series opener",
      "pubDate":"2017-06-24 11:30:20",
      "link":"http://www.cnn.com/2017/06/24/sport/new-zealand-british-and-irish-lions-rugby-eden-park-auckland/index.html",
      "guid":"http://www.cnn.com/2017/06/24/sport/new-zealand-british-and-irish-lions-rugby-eden-park-auckland/index.html",
      "author":"",
      "thumbnail":"",
      "description":"For the touring British and Irish Lions, the early signs are ominous.",
      "content":"For the touring British and Irish Lions, the early signs are ominous.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170624095656-codie-taylor-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Violent protests spread in India's Darjeeling tea lands",
      "pubDate":"2017-06-23 09:38:31",
      "link":"http://www.cnn.com/2017/06/23/asia/darjeeling-crisis-india/index.html",
      "guid":"http://www.cnn.com/2017/06/23/asia/darjeeling-crisis-india/index.html",
      "author":"",
      "thumbnail":"",
      "description":"The sleepy hills of India's northeast have erupted into violence, as calls for a separate state for the area's Nepali-speaking Gorkhas gain traction.",
      "content":"The sleepy hills of India's northeast have erupted into violence, as calls for a separate state for the area's Nepali-speaking Gorkhas gain traction.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623102753-gorkha-protest-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Air Force 'doomsday' planes damaged in tornado",
      "pubDate":"2017-06-23 22:09:54",
      "link":"http://www.cnn.com/2017/06/23/politics/air-force-planes-damaged-tornado/index.html",
      "guid":"http://www.cnn.com/2017/06/23/politics/air-force-planes-damaged-tornado/index.html",
      "author":"",
      "thumbnail":"",
      "description":"Ten US Air Force planes, including two E-4B \"doomsday\" aircraft, were damaged when a tornado hit near Offutt Air Force Base in Nebraska last week, the service told CNN on Friday.",
      "content":"Ten US Air Force planes, including two E-4B \"doomsday\" aircraft, were damaged when a tornado hit near Offutt Air Force Base in Nebraska last week, the service told CNN on Friday.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623173900-e-4b-national-airborne-operations-center-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"South Korea's new presidential pooches, and why they matter",
      "pubDate":"2017-06-24 02:54:17",
      "link":"http://www.cnn.com/2017/06/23/opinions/south-korea-dogs-animal-welfare/index.html",
      "guid":"http://www.cnn.com/2017/06/23/opinions/south-korea-dogs-animal-welfare/index.html",
      "author":"",
      "thumbnail":"",
      "description":"When South Korea's new president Moon Jae-in took up residency in the Blue House last month, his choice of pets raised some eyebrows.",
      "content":"When South Korea's new president Moon Jae-in took up residency in the Blue House last month, his choice of pets raised some eyebrows.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170606205829-s-korea-dog-breeders-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Al Jazeera: What you need to know",
      "pubDate":"2017-06-23 16:01:18",
      "link":"http://www.cnn.com/videos/cnnmoney/2017/06/23/al-jazeera-explainer-mxb-lon-orig.cnnmoney",
      "guid":"http://www.cnn.com/videos/cnnmoney/2017/06/23/al-jazeera-explainer-mxb-lon-orig.cnnmoney",
      "author":"",
      "thumbnail":"",
      "description":"CNN's Frederik Pleitgen takes a look at the controversies surrounding this global news network based in Qatar.",
      "content":"CNN's Frederik Pleitgen takes a look at the controversies surrounding this global news network based in Qatar.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623165833-al-jazeera-1-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"The world will solve its own problems -- with or without Trump",
      "pubDate":"2017-06-23 11:36:51",
      "link":"http://www.cnn.com/2017/06/23/opinions/us-global-leadership-robertson/index.html",
      "guid":"http://www.cnn.com/2017/06/23/opinions/us-global-leadership-robertson/index.html",
      "author":"",
      "thumbnail":"",
      "description":"It's been a blistering week in London, with little shade from the sun or the news.",
      "content":"It's been a blistering week in London, with little shade from the sun or the news.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170602164707-trump-world-leaders-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"US releases dramatic photos of 'unsafe' Russian jet intercept",
      "pubDate":"2017-06-23 18:16:02",
      "link":"http://www.cnn.com/2017/06/23/politics/russian-jet-intercepts-us-aircraft-photos/index.html",
      "guid":"http://www.cnn.com/2017/06/23/politics/russian-jet-intercepts-us-aircraft-photos/index.html",
      "author":"",
      "thumbnail":"",
      "description":"US European Command released dramatic photos on Thursday that show the moment a Russian Su-27 fighter jet flew within five feet of a US Air Force RC-135 reconnaissance aircraft over the Baltic Sea -- an encounter US officials assessed to be \"unsafe.\"",
      "content":"US European Command released dramatic photos on Thursday that show the moment a Russian Su-27 fighter jet flew within five feet of a US Air Force RC-135 reconnaissance aircraft over the Baltic Sea -- an encounter US officials assessed to be \"unsafe.\"",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623121807-russian-unsafe-intercept-01-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"May's Brexit divorce deal branded 'not sufficient'",
      "pubDate":"2017-06-23 13:40:08",
      "link":"http://www.cnn.com/2017/06/23/europe/eu-nationals-brexit-may/index.html",
      "guid":"http://www.cnn.com/2017/06/23/europe/eu-nationals-brexit-may/index.html",
      "author":"",
      "thumbnail":"",
      "description":"British Prime Minister Theresa May's plans to secure the rights of EU nationals after Brexit have been criticized as \"not sufficient,\" by the head of the European Commission.",
      "content":"British Prime Minister Theresa May's plans to secure the rights of EU nationals after Brexit have been criticized as \"not sufficient,\" by the head of the European Commission.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623094855-04-theresa-may-brussels-0623-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"No break for Britain: 5 tragedies in 3 months",
      "pubDate":"",
      "link":"http://cnn.it/2rLuYi4",
      "guid":"http://cnn.it/2rLuYi4",
      "author":"",
      "thumbnail":"",
      "description":"",
      "content":"",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170623162903-01-uk-tragedies-cnnphotos-super-169.jpg"
      },
      "categories":[

      ]
   },
   {
      "title":"Splash 'dancing' at the Dallas Zoo with Zola the gorilla",
      "pubDate":"2017-06-24 11:51:21",
      "link":"http://www.cnn.com/2017/06/24/us/dallas-zoo-dancing-gorilla-trnd/index.html",
      "guid":"http://www.cnn.com/2017/06/24/us/dallas-zoo-dancing-gorilla-trnd/index.html",
      "author":"",
      "thumbnail":"",
      "description":"There seems to be a lot more going on behind the scenes at the Dallas Zoo -- including some splashy dance moves by Zola the gorilla.",
      "content":"There seems to be a lot more going on behind the scenes at the Dallas Zoo -- including some splashy dance moves by Zola the gorilla.",
      "enclosure":{
         "link":"http://i2.cdn.turner.com/cnnnext/dam/assets/170624051705-dallas-zoo-gorilla-zola-splash-pool-sot-nr-00002328-super-169.jpg"
      },
      "categories":[

      ]
   }]

这就是我试过的

[WebMethod]
    public string RSSfeeddata(string info) {

        SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
        var outObject = JsonConvert.DeserializeObject(info);
        DataSet myDataSet = JsonConvert.DeserializeObject<DataSet>(info);     
        return info;
    }

1 个答案:

答案 0 :(得分:1)

您可以将Json反序列化为数据集的唯一方法是首先从数据集中序列化它。相反,您可以创建一个与您拥有的Json一起使用的类。为此,将Json复制到剪贴板,在Visual Studio中创建一个新的.json文件,单击编辑菜单,转到粘贴特殊,然后粘贴JSON作为类。对我来说它产生了......

public class Class1
{
    public string title { get; set; }
    public string pubDate { get; set; }
    public string link { get; set; }
    public string guid { get; set; }
    public string author { get; set; }
    public string thumbnail { get; set; }
    public string description { get; set; }
    public string content { get; set; }
    public Enclosure enclosure { get; set; }
    public object[] categories { get; set; }
}

public class Enclosure
{
    public string link { get; set; }
}

然后你将json读成IEnumerable<Class1>(我没有重命名该类),如下所示

var text = System.IO.File.ReadAllText("json1.json");

var items = JsonConvert.DeserializeObject<IEnumerable<Class1>>(text);

然后,您可以使用该IEnumerable代替数据集。