JSON.NET对对象/对象数组中的对象进行反序列化 [英] JSON.NET Deserialize objects within object / array of objects

查看:87
本文介绍了JSON.NET对对象/对象数组中的对象进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我正在使用的API返回不一致的JSON,我想使用JSON.NET反序列化.在一种情况下,它返回一个包含对象的对象(请注意,外部的"1"可以是任何数字):

I have a situation where an API I'm using is returning inconsistent JSON, which I want to deserialize using JSON.NET. In one case, it returns an object that contains objects (note that the outer "1" can be any number):

{
   "1":{
      "0":{
         "db_id":"12835424",
         "title":"XXX"
      },
      "1":{
         "db_id":"12768978",
         "title":"YYY"
      },
      "2":{
         "db_id":"12768980",
         "title":"ZZZ"
      },
      "3":{
         "db_id":"12768981",
         "title":"PPP"
      }
   }
}

在另一种情况下,它返回一个对象数组:

And in another case, it returns an array of objects:

{
   "3":[
      {
         "db_id":"12769199",
         "title":"XXX"
      },
      {
         "db_id":"12769200",
         "title":"YYY"
      },
      {
         "db_id":"12769202",
         "title":"ZZZ"
      },
      {
         "db_id":"12769243",
         "title":"PPP"
      }
   ]
}

我不知道为什么存在这种不一致,但这是我正在使用的格式.用JsonConvert.DeserializeObject方法反序列化这两种格式的正确方法是什么?

I have no idea why this inconsistency exists, but this is the format I'm working with. What would be the correct way to deserialize both formats with the JsonConvert.DeserializeObject method?

推荐答案

我认为这可以通过创建 JsonCreationConverter 来实现.本文可能可以帮助您: http://dotnetbyexample. blogspot.nl/2012/02/json-deserialization-with-jsonnet-class.html

I think this is something that should be possible by creating a JsonCreationConverter. This article can probably help out: http://dotnetbyexample.blogspot.nl/2012/02/json-deserialization-with-jsonnet-class.html

这篇关于JSON.NET对对象/对象数组中的对象进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆