【问题标题】:json jarray error due to response由于响应导致的json jarray错误
【发布时间】:2014-02-17 12:55:59
【问题描述】:

由于开头的响应数据,我正在访问的 Json 不会让我转换为我认为的 jarray

    {
"response":{
"status":"ok",
"userTier":"approved",
"total":275,
"startIndex":1,
"pageSize":10,
"currentPage":1,
"pages":28,
"orderBy":"newest",
"results":[{
  "id":"guardian-observer-style-guide-b",
  "sectionId":"info",
  "sectionName":"Info",
  "webPublicationDate":"2014-02-14T14:52:00Z",
  "webTitle":"Guardian and Observer style guide: B",
  "webUrl":"http://www.theguardian.com/guardian-observer-style-guide-b",
  "apiUrl":"http://content.guardianapis.com/guardian-observer-style-guide-b"
},{
  "id":"sustainable-business/sustainable-finance-where-next-open-thread",
  "sectionId":"sustainable-business",
  "sectionName":"Guardian Sustainable Business",
  "webPublicationDate":"2014-02-13T13:27:00Z",
  "webTitle":"Where next for sustainable finance? - open thread",
  "webUrl":"http://www.theguardian.com/sustainable-business/sustainable-finance-where-next-open-thread",
  "apiUrl":"http://content.guardianapis.com/sustainable-business/sustainable-finance-where-next-open-thread"
},{
  "id":"media/2014/feb/13/media-briefing-thursday-13-february",
  "sectionId":"media",
  "sectionName":"Media",
  "webPublicationDate":"2014-02-13T08:48:28Z",
  "webTitle":"Today's media stories from the papers",
  "webUrl":"http://www.theguardian.com/media/2014/feb/13/media-briefing-thursday-13-february",
  "apiUrl":"http://content.guardianapis.com/media/2014/feb/13/media-briefing-thursday-13-february"
},{
  "id":"technology/2014/feb/12/bitcoin-exchanges-suspend-conversions-hacker-attack",
  "sectionId":"technology",
  "sectionName":"Technology",
  "webPublicationDate":"2014-02-12T19:38:31Z",
  "webTitle":"Bitcoin exchanges suspend conversions as hacker attack intensifies",
  "webUrl":"http://www.theguardian.com/technology/2014/feb/12/bitcoin-exchanges-suspend-conversions-hacker-attack",
  "apiUrl":"http://content.guardianapis.com/technology/2014/feb/12/bitcoin-exchanges-suspend-conversions-hacker-attack"
},{
  "id":"sustainable-business/sustainable-finance-top-ten-tweeters-twitter",
  "sectionId":"sustainable-business",
  "sectionName":"Guardian Sustainable Business",
  "webPublicationDate":"2014-02-12T12:31:00Z",
  "webTitle":"Twitter list: 10 top sustainable finance tweeters ",
  "webUrl":"http://www.theguardian.com/sustainable-business/sustainable-finance-top-ten-tweeters-twitter",
  "apiUrl":"http://content.guardianapis.com/sustainable-business/sustainable-finance-top-ten-tweeters-twitter"
},{
  "id":"media/2014/feb/11/media-briefing-tuesday-11-february",
  "sectionId":"media",
  "sectionName":"Media",
  "webPublicationDate":"2014-02-11T09:24:13Z",
  "webTitle":"Today's media stories from the papers",
  "webUrl":"http://www.theguardian.com/media/2014/feb/11/media-briefing-tuesday-11-february",
  "apiUrl":"http://content.guardianapis.com/media/2014/feb/11/media-briefing-tuesday-11-february"
},{
  "id":"technology/blog/2014/feb/11/app-purchase-stats-secret",
  "sectionId":"technology",
  "sectionName":"Technology",
  "webPublicationDate":"2014-02-11T07:30:00Z",
  "webTitle":"Boot up: app purchase stats, Secret v anonymity, 4G in China, and more",
  "webUrl":"http://www.theguardian.com/technology/blog/2014/feb/11/app-purchase-stats-secret",
  "apiUrl":"http://content.guardianapis.com/technology/blog/2014/feb/11/app-purchase-stats-secret"
},{
  "id":"technology/2014/feb/10/how-ransomware-turns-your-computer-bitcoin-miner-linkup",
  "sectionId":"technology",
  "sectionName":"Technology",
  "webPublicationDate":"2014-02-10T11:00:26Z",
  "webTitle":"How Ransomware turns your computer into a bitcoin miner",
  "webUrl":"http://www.theguardian.com/technology/2014/feb/10/how-ransomware-turns-your-computer-bitcoin-miner-linkup",
  "apiUrl":"http://content.guardianapis.com/technology/2014/feb/10/how-ransomware-turns-your-computer-bitcoin-miner-linkup"
},{
  "id":"technology/2014/feb/10/cody-wilson-3d-gun-anarchist",
  "sectionId":"technology",
  "sectionName":"Technology",
  "webPublicationDate":"2014-02-10T07:00:00Z",
  "webTitle":"Meet Cody Wilson, creator of the 3D-gun, anarchist, libertarian",
  "webUrl":"http://www.theguardian.com/technology/2014/feb/10/cody-wilson-3d-gun-anarchist",
  "apiUrl":"http://content.guardianapis.com/technology/2014/feb/10/cody-wilson-3d-gun-anarchist"
},{
  "id":"media/2014/feb/07/media-briefing-friday-7-february",
  "sectionId":"media",
  "sectionName":"Media",
  "webPublicationDate":"2014-02-07T08:41:11Z",
  "webTitle":"Today's media stories from the papers",
  "webUrl":"http://www.theguardian.com/media/2014/feb/07/media-briefing-friday-7-february",
  "apiUrl":"http://content.guardianapis.com/media/2014/feb/07/media-briefing-friday-7-february"
}]
}
}

我该如何解决这个问题?我曾尝试使用 JObject,但它只返回我的数组中的一个对象

谢谢

【问题讨论】:

  • 如果您发布您的代码会很有帮助,这样我们就可以看到您正在尝试做什么。

标签: c# json json.net


【解决方案1】:
JObject json = JObject.Parse (yourstring); JArray 结果 = json.SelectToken("response").SelectToken("results");

【讨论】:

  • 无法将类型 jtoken 转换为 jarray
  • 我必须以某种方式转换它吗?
  • JToken 是 jobject,jarray,jvalue 的基类,所以 jtoken 可以同时处理 jarray 和 jtoken 。 jarry 仅在类型为 jarray 时才解析 jtoken
  • 错误 1 ​​无法将类型“Newtonsoft.Json.Linq.JToken”隐式转换为“Newtonsoft.Json.Linq.JArray”。存在显式转换(您是否缺少演员表?)
  • 我完全按照你说的做了,但不幸的是遇到了这个错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2018-12-09
  • 1970-01-01
相关资源
最近更新 更多