【问题标题】:Deserializing JToken result using JSON.net使用 JSON.net 反序列化 JToken 结果
【发布时间】:2012-12-17 07:00:18
【问题描述】:

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

【问题讨论】:

    标签: c# json.net


    【解决方案1】:
    1. jToken.First 将删除 "60": {,只留下键值对。

    2. jToken.First.ToString() 上使用IDictionary 而不是KeyValuePair&lt;string, string&gt;&gt;,这将解决您的问题。

    让我知道这是否有效

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 2019-05-20
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多