0

使用 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 属性的方法?

4

1 回答 1

1
  1. jToken.First将删除"60": {只留下键值对的内容。

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

让我知道这个是否奏效

于 2012-12-17T12:52:09.713 回答