使用JSON.net反序列化JToken结果

时间:2012-12-17 07:00:18

标签: c# json.net

使用Newtonsoft JSON.net,我有以下JToken结果:

"60": {
  "maps": false,
  "real_time": false,
  "video_pack_results": {},
  "indented": 0,
  "blog_posts": false,
  "description": "A LONDON household is celebrating a U-turn by an insurance company that   means the occupants no longer have to foot half a pounds 25000 bill for ...",
  "base_url": "www.independent.co.uk",
  "place_pack": {},
  "dirty_url": "/url?q=http://www.independent.co.uk/news/business/subsidence-leads-to-an-insurance-fallout-transfer-of-property-freehold-leaves-flat-owners-without-full-cover-1481999.html&sa=U&ei=qiPFUIv2J4WJiwLc3ICQAQ&ved=0CKMBEBYwJzgU&usg=AFQjCNFbJ0NSAqXOnBdAViKZXQidPK1vEg",
  "video": false,
  "href": "http://www.independent.co.uk/news/business/subsidence-leads-to-an-insurance-fallout-transfer-of-property-freehold-leaves-flat-owners-without-full-cover-1481999.html",
  "title": "Subsidence leads to an insurance fall-out: Transfer of property ...",
  "news": false,
  "shopping": false,
  "micro_format": false,
  "image": false,
  "place_details": {},
  "image_pack_results": [],
  "url": "http://www.independent.co.uk/news/business/subsidence-leads-to-an-insurance-fallout-transfer-of-property-freehold-leaves-flat-owners-without-full-cover-1481999.html",
  "brand_pack_results": {},
  "local_pack": {},
  "page": 7,
  "base_domain": "independent.co.uk"
}   

我正在尝试使用以下方法从JSON.net解析时检索值“60”:

var keyValue = JsonConvert.DeserializeObject<KeyValuePair<string, string>>(jToken.ToString());

我遇到的第一个错误是缺少围绕初始内容的“{”“}”标签,我最初将其附加到JToken的原始JSON字符串中。结果是一个空的KeyValuePair类型。

是否有人知道以上述格式解析JToken以分离Key和Value属性的方法?

1 个答案:

答案 0 :(得分:1)

  1. jToken.First会删除只留给键值对的"60": {

  2. IDictionary使用KeyValuePair<string, string>>而不是jToken.First.ToString()来解决您的问题。

  3. 让我知道这是否有效