2017-10-09 104 views
3

我一直在研究API的複雜JSON響應的反序列化。我是C#和.NET新手,所以我不知道如何繼續。將複雜(嵌套)JSON反序列化爲C#對象

我想要做什麼:

我想收集在JSON響應中的所有項目,並把他們在鍵值對,並在列表中用於測試目的的控制檯應用程序歸還。

當我調試時它工作得很好,我可以看到它按預期映射了所有鍵值對。我的努力是在控制檯應用程序中返回列表中的所有內容。

這是我的「模型」類的代碼。

​​

這是我的代碼「WriteLine」命令嘗試列出列表中的所有鍵值對。

using System; 

class ConsoleAppClass 
{ 
    static void Main() 
    { 
     string json2 = @"{""code"":200,""message"":""OK"",""profiling"":null,""count"":6,""skip"":0,""limit"":50,""next_page"":null,""previous_page"":null,""additional_meta"":{""$and"":[{""deleted"":false},{""$or"":[{""holder_id"":{""$id"":""5543677f37c6b65f008b46e4""}},{""root.holder._id"":{""$id"":""5543677f37c6b65f008b46e4""}}]}]},""data"":[{""_id"":""5825c6f6de22ae00632e86f3"",""ean"":""659842"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""5824790e3d214b006271c515"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:26:14+0000"",""children"":[],""address"":""Kungsgatan 2"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""heat"",""consumption_stats"":{""energy"":{""hour"":{""count"":26275,""sum"":6546140,""max"":930,""last"":2016123021,""min"":0,""first"":2014010101},""day"":{""count"":1091,""sum"":6522120,""max"":18900,""last"":20161229,""min"":1180,""first"":20140102},""month"":{""count"":33,""sum"":5727310,""max"":387190,""last"":201708,""min"":49750,""first"":201402}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c76920fe9b006308e412"",""ean"":""735999999564879216"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""5824790e3d214b006271c510"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:28:09+0000"",""children"":[],""address"":""Kungsgatan 2"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""electricity"",""consumption_stats"":{""energy"":{""hour"":{""count"":25953,""sum"":311714.628,""max"":34.3,""last"":2016121709,""min"":0,""first"":2014010101},""day"":{""count"":1080,""sum"":311302.328,""max"":689,""last"":20161216,""min"":141.1,""first"":20140102},""month"":{""count"":35,""sum"":306915.54,""max"":13704.72,""last"":201708,""min"":6013.932,""first"":201402}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c84b22c8aa00623d65a5"",""ean"":""6934363"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b4672"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:31:55+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""gas"",""consumption_stats"":{""energy"":{""month"":{""count"":24,""sum"":42656.62,""max"":8890.65,""last"":201708,""min"":104.57,""first"":201501},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c85320fe9b006308e41d"",""ean"":""5896584"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b466c"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:32:03+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""heat"",""consumption_stats"":{""energy"":{""month"":{""count"":57,""sum"":3592320,""max"":173000,""last"":201709,""min"":2000,""first"":201301},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c85920fe9b0061547253"",""ean"":""735999666123123123"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b4669"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:32:09+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""electricity"",""consumption_stats"":{""energy"":{""month"":{""count"":45,""sum"":141604.36,""max"":6488.2,""last"":201709,""min"":1759.64,""first"":201401},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c85d22c8aa00606b9dd5"",""ean"":""1200004"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b466f"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:32:13+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""cooling"",""consumption_stats"":{""energy"":{""month"":{""count"":33,""sum"":902350,""max"":92560,""last"":201709,""min"":9730,""first"":201501},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null}]}"; 

     var items = JsonConvert.DeserializeObject<List<ModelsClass.Rootobject>>(json2); 
     foreach (ModelsClass.Rootobject value in items) 
     { 
      Console.WriteLine(value, items); 
     } 
     Console.ReadKey(); 
    } 
} 

那麼,誰能幫我我如何從我的模型類返回我的所有鍵值對的命令行?如你所見,JSON響應對於混合對象和數組非常複雜。

回答

1

1-你應該從你的JSON文本刪除$字符

2-根節點是不是數組,這就是爲什麼像這樣的

JsonConvert.DeserializeObject<ConsoleApplication2.Rootobject>(json2); 

3-變化反序列化操作您可以使用此功能打印名稱 - 值對。

private static void printPropertyNameValue(object obj, int intent) 
    { 
     foreach (var property in obj.GetType().GetProperties()) 
     { 
      if (!property.CanWrite) 
       continue; 

      var value = property.GetValue(obj); 

      if (value != null && value.GetType().IsArray) 
      { 
       Console.WriteLine("{0}{1}", "".PadLeft(intent, '-'), property.Name); 
       foreach (var item in ((Object[])value)) 
       { 
        printPropertyNameValue(item, intent + 1); 
       } 
      } 
      else 
      { 
       if (value != null && (value.GetType()).IsClass) 
       { 
        Console.WriteLine("{0}{1}", "".PadLeft(intent, '-'), property.Name); 
        printPropertyNameValue(property.GetValue(obj), intent + 1); 
       } 
       else 
       { 
        Console.WriteLine("{0}{1}:{2}", "".PadLeft(intent, '-'), property.Name, value); 
       } 
      } 
     } 
    } 

4和最終的主要功能是這樣的:

static void Main(string[] args) 
    { 

     string json2 = @"{""code"":200,""message"":""OK"",""profiling"":null,""count"":6,""skip"":0,""limit"":50,""next_page"":null,""previous_page"":null,""additional_meta"":{""and"":[{""deleted"":false},{""or"":[{""holder_id"":{""id"":""5543677f37c6b65f008b46e4""}},{""root.holder._id"":{""id"":""5543677f37c6b65f008b46e4""}}]}]},""data"":[{""_id"":""5825c6f6de22ae00632e86f3"",""ean"":""659842"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""5824790e3d214b006271c515"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:26:14+0000"",""children"":[],""address"":""Kungsgatan 2"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""heat"",""consumption_stats"":{""energy"":{""hour"":{""count"":26275,""sum"":6546140,""max"":930,""last"":2016123021,""min"":0,""first"":2014010101},""day"":{""count"":1091,""sum"":6522120,""max"":18900,""last"":20161229,""min"":1180,""first"":20140102},""month"":{""count"":33,""sum"":5727310,""max"":387190,""last"":201708,""min"":49750,""first"":201402}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c76920fe9b006308e412"",""ean"":""735999999564879216"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""5824790e3d214b006271c510"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:28:09+0000"",""children"":[],""address"":""Kungsgatan 2"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""electricity"",""consumption_stats"":{""energy"":{""hour"":{""count"":25953,""sum"":311714.628,""max"":34.3,""last"":2016121709,""min"":0,""first"":2014010101},""day"":{""count"":1080,""sum"":311302.328,""max"":689,""last"":20161216,""min"":141.1,""first"":20140102},""month"":{""count"":35,""sum"":306915.54,""max"":13704.72,""last"":201708,""min"":6013.932,""first"":201402}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c84b22c8aa00623d65a5"",""ean"":""6934363"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b4672"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:31:55+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""gas"",""consumption_stats"":{""energy"":{""month"":{""count"":24,""sum"":42656.62,""max"":8890.65,""last"":201708,""min"":104.57,""first"":201501},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c85320fe9b006308e41d"",""ean"":""5896584"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b466c"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:32:03+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""heat"",""consumption_stats"":{""energy"":{""month"":{""count"":57,""sum"":3592320,""max"":173000,""last"":201709,""min"":2000,""first"":201301},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c85920fe9b0061547253"",""ean"":""735999666123123123"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b4669"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:32:09+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""electricity"",""consumption_stats"":{""energy"":{""month"":{""count"":45,""sum"":141604.36,""max"":6488.2,""last"":201709,""min"":1759.64,""first"":201401},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null},{""_id"":""5825c85d22c8aa00606b9dd5"",""ean"":""1200004"",""name"":null,""holder"":{""_id"":""5543677f37c6b65f008b46e4"",""name"":""Example account"",""username"":""[email protected]""},""root"":{""_id"":""577fdd960c8a335c008b466f"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""[email protected]"",""manager"":{""_id"":null}},""created"":""2016-11-11T13:32:13+0000"",""children"":[],""address"":""Gr\u00e4nsv\u00e4gen 12B"",""timezone"":""Etc\/GMT-1"",""control_level"":""shared"",""billing_category"":""owned_meter_consumption"",""generation"":1,""tags"":[],""box"":""active"",""revoked"":false,""metrics"":[""energy""],""type"":""cooling"",""consumption_stats"":{""energy"":{""month"":{""count"":33,""sum"":902350,""max"":92560,""last"":201709,""min"":9730,""first"":201501},""hour"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0},""day"":{""first"":null,""last"":null,""sum"":null,""max"":null,""min"":null,""count"":0}}},""cost_stats"":{},""readings_stats"":null,""has_active_complaints"":false,""representation"":null,""open_channel_ids"":null,""on_hold"":null,""last_sla_comment"":null,""location"":null,""meta_data"":{},""meter_readers"":[],""collection_window"":5,""memo"":null}]}"; 

     var items = JsonConvert.DeserializeObject<ConsoleApplication2.Rootobject>(json2); 

     printPropertyNameValue(items, 0); 

     Console.ReadKey(); 
    } 
0

獲取所有類型&屬性從你的類 類似下面

Dictionary<string, string> myDict = new Dictionary<string, string>(); 
Type t = data.GetType(); 

    foreach (PropertyInfo pi in t.GetProperties()) 
    { 
     myDict[pi.Name] = pi.GetValue(data,null).ToString(); 
    } 
0

(做了一個分開的一篇關於這一點,因爲在字符限制)

如果你想看到格式化的JSON示例反應,它是下面:

{ 
    "code":200, 
    "message":"OK", 
    "profiling":null, 
    "count":6, 
    "skip":0, 
    "limit":50, 
    "next_page":null, 
    "previous_page":null, 
    "additional_meta":{ 
     "$and":[ 
     { 
      "deleted":false 
     }, 
     { 
      "$or":[ 
       { 
        "holder_id":{ 
        "$id":"5543677f37c6b65f008b46e4" 
        } 
       }, 
       { 
        "root.holder._id":{ 
        "$id":"5543677f37c6b65f008b46e4" 
        } 
       } 
      ] 
     } 
     ] 
    }, 
    "data":[ 
     { 
     "_id":"5825c6f6de22ae00632e86f3", 
     "ean":"659842", 
     "name":null, 
     "holder":{ 
      "_id":"5543677f37c6b65f008b46e4", 
      "name":"Example account", 
      "username":"[email protected]" 
     }, 
     "root":{ 
      "_id":"5824790e3d214b006271c515", 
      "holder":{ 
       "_id":"577fd0170c8a335c008b45fe", 
       "name":"Rob Harvey Utility Company", 
       "entity_type":"utility_provider" 
      } 
     }, 
     "assigner":{ 
      "_id":"577fd0170c8a335c008b45fe", 
      "name":"Rob Harvey Utility Company", 
      "username":"[email protected]", 
      "manager":{ 
       "_id":null 
      } 
     }, 
     "created":"2016-11-11T13:26:14+0000", 
     "children":[ 

     ], 
     "address":"Kungsgatan 2", 
     "timezone":"Etc\/GMT-1", 
     "control_level":"shared", 
     "billing_category":"owned_meter_consumption", 
     "generation":1, 
     "tags":[ 

     ], 
     "box":"active", 
     "revoked":false, 
     "metrics":[ 
      "energy" 
     ], 
     "type":"heat", 
     "consumption_stats":{ 
      "energy":{ 
       "hour":{ 
        "count":26275, 
        "sum":6546140, 
        "max":930, 
        "last":2016123021, 
        "min":0, 
        "first":2014010101 
       }, 
       "day":{ 
        "count":1091, 
        "sum":6522120, 
        "max":18900, 
        "last":20161229, 
        "min":1180, 
        "first":20140102 
       }, 
       "month":{ 
        "count":33, 
        "sum":5727310, 
        "max":387190, 
        "last":201708, 
        "min":49750, 
        "first":201402 
       } 
      } 
     }, 
     "cost_stats":{ 

     }, 
     "readings_stats":null, 
     "has_active_complaints":false, 
     "representation":null, 
     "open_channel_ids":null, 
     "on_hold":null, 
     "last_sla_comment":null, 
     "location":null, 
     "meta_data":{ 

     }, 
     "meter_readers":[ 

     ], 
     "collection_window":5, 
     "memo":null 
     }, 
     { 
     "_id":"5825c76920fe9b006308e412", 
     "ean":"735999999564879216", 
     "name":null, 
     "holder":{ 
      "_id":"5543677f37c6b65f008b46e4", 
      "name":"Example account", 
      "username":"[email protected]" 
     }, 
     "root":{ 
      "_id":"5824790e3d214b006271c510", 
      "holder":{ 
       "_id":"577fd0170c8a335c008b45fe", 
       "name":"Rob Harvey Utility Company", 
       "entity_type":"utility_provider" 
      } 
     }, 
     "assigner":{ 
      "_id":"577fd0170c8a335c008b45fe", 
      "name":"Rob Harvey Utility Company", 
      "username":"[email protected]", 
      "manager":{ 
       "_id":null 
      } 
     }, 
     "created":"2016-11-11T13:28:09+0000", 
     "children":[ 

     ], 
     "address":"Kungsgatan 2", 
     "timezone":"Etc\/GMT-1", 
     "control_level":"shared", 
     "billing_category":"owned_meter_consumption", 
     "generation":1, 
     "tags":[ 

     ], 
     "box":"active", 
     "revoked":false, 
     "metrics":[ 
      "energy" 
     ], 
     "type":"electricity", 
     "consumption_stats":{ 
      "energy":{ 
       "hour":{ 
        "count":25953, 
        "sum":311714.628, 
        "max":34.3, 
        "last":2016121709, 
        "min":0, 
        "first":2014010101 
       }, 
       "day":{ 
        "count":1080, 
        "sum":311302.328, 
        "max":689, 
        "last":20161216, 
        "min":141.1, 
        "first":20140102 
       }, 
       "month":{ 
        "count":35, 
        "sum":306915.54, 
        "max":13704.72, 
        "last":201708, 
        "min":6013.932, 
        "first":201402 
       } 
      } 
     }, 
     "cost_stats":{ 

     }, 
     "readings_stats":null, 
     "has_active_complaints":false, 
     "representation":null, 
     "open_channel_ids":null, 
     "on_hold":null, 
     "last_sla_comment":null, 
     "location":null, 
     "meta_data":{ 

     }, 
     "meter_readers":[ 

     ], 
     "collection_window":5, 
     "memo":null 
     }, 
     { 
     "_id":"5825c84b22c8aa00623d65a5", 
     "ean":"6934363", 
     "name":null, 
     "holder":{ 
      "_id":"5543677f37c6b65f008b46e4", 
      "name":"Example account", 
      "username":"[email protected]" 
     }, 
     "root":{ 
      "_id":"577fdd960c8a335c008b4672", 
      "holder":{ 
       "_id":"577fd0170c8a335c008b45fe", 
       "name":"Rob Harvey Utility Company", 
       "entity_type":"utility_provider" 
      } 
     }, 
     "assigner":{ 
      "_id":"577fd0170c8a335c008b45fe", 
      "name":"Rob Harvey Utility Company", 
      "username":"[email protected]", 
      "manager":{ 
       "_id":null 
      } 
     }, 
     "created":"2016-11-11T13:31:55+0000", 
     "children":[ 

     ], 
     "address":"Gr\u00e4nsv\u00e4gen 12B", 
     "timezone":"Etc\/GMT-1", 
     "control_level":"shared", 
     "billing_category":"owned_meter_consumption", 
     "generation":1, 
     "tags":[ 

     ], 
     "box":"active", 
     "revoked":false, 
     "metrics":[ 
      "energy" 
     ], 
     "type":"gas", 
     "consumption_stats":{ 
      "energy":{ 
       "month":{ 
        "count":24, 
        "sum":42656.62, 
        "max":8890.65, 
        "last":201708, 
        "min":104.57, 
        "first":201501 
       }, 
       "hour":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       }, 
       "day":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       } 
      } 
     }, 
     "cost_stats":{ 

     }, 
     "readings_stats":null, 
     "has_active_complaints":false, 
     "representation":null, 
     "open_channel_ids":null, 
     "on_hold":null, 
     "last_sla_comment":null, 
     "location":null, 
     "meta_data":{ 

     }, 
     "meter_readers":[ 

     ], 
     "collection_window":5, 
     "memo":null 
     }, 
     { 
     "_id":"5825c85320fe9b006308e41d", 
     "ean":"5896584", 
     "name":null, 
     "holder":{ 
      "_id":"5543677f37c6b65f008b46e4", 
      "name":"Example account", 
      "username":"[email protected]" 
     }, 
     "root":{ 
      "_id":"577fdd960c8a335c008b466c", 
      "holder":{ 
       "_id":"577fd0170c8a335c008b45fe", 
       "name":"Rob Harvey Utility Company", 
       "entity_type":"utility_provider" 
      } 
     }, 
     "assigner":{ 
      "_id":"577fd0170c8a335c008b45fe", 
      "name":"Rob Harvey Utility Company", 
      "username":"[email protected]", 
      "manager":{ 
       "_id":null 
      } 
     }, 
     "created":"2016-11-11T13:32:03+0000", 
     "children":[ 

     ], 
     "address":"Gr\u00e4nsv\u00e4gen 12B", 
     "timezone":"Etc\/GMT-1", 
     "control_level":"shared", 
     "billing_category":"owned_meter_consumption", 
     "generation":1, 
     "tags":[ 

     ], 
     "box":"active", 
     "revoked":false, 
     "metrics":[ 
      "energy" 
     ], 
     "type":"heat", 
     "consumption_stats":{ 
      "energy":{ 
       "month":{ 
        "count":57, 
        "sum":3592320, 
        "max":173000, 
        "last":201709, 
        "min":2000, 
        "first":201301 
       }, 
       "hour":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       }, 
       "day":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       } 
      } 
     }, 
     "cost_stats":{ 

     }, 
     "readings_stats":null, 
     "has_active_complaints":false, 
     "representation":null, 
     "open_channel_ids":null, 
     "on_hold":null, 
     "last_sla_comment":null, 
     "location":null, 
     "meta_data":{ 

     }, 
     "meter_readers":[ 

     ], 
     "collection_window":5, 
     "memo":null 
     }, 
     { 
     "_id":"5825c85920fe9b0061547253", 
     "ean":"735999666123123123", 
     "name":null, 
     "holder":{ 
      "_id":"5543677f37c6b65f008b46e4", 
      "name":"Example account", 
      "username":"[email protected]" 
     }, 
     "root":{ 
      "_id":"577fdd960c8a335c008b4669", 
      "holder":{ 
       "_id":"577fd0170c8a335c008b45fe", 
       "name":"Rob Harvey Utility Company", 
       "entity_type":"utility_provider" 
      } 
     }, 
     "assigner":{ 
      "_id":"577fd0170c8a335c008b45fe", 
      "name":"Rob Harvey Utility Company", 
      "username":"RobHarveyUtilityCompany-utility[email protected]", 
      "manager":{ 
       "_id":null 
      } 
     }, 
     "created":"2016-11-11T13:32:09+0000", 
     "children":[ 

     ], 
     "address":"Gr\u00e4nsv\u00e4gen 12B", 
     "timezone":"Etc\/GMT-1", 
     "control_level":"shared", 
     "billing_category":"owned_meter_consumption", 
     "generation":1, 
     "tags":[ 

     ], 
     "box":"active", 
     "revoked":false, 
     "metrics":[ 
      "energy" 
     ], 
     "type":"electricity", 
     "consumption_stats":{ 
      "energy":{ 
       "month":{ 
        "count":45, 
        "sum":141604.36, 
        "max":6488.2, 
        "last":201709, 
        "min":1759.64, 
        "first":201401 
       }, 
       "hour":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       }, 
       "day":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       } 
      } 
     }, 
     "cost_stats":{ 

     }, 
     "readings_stats":null, 
     "has_active_complaints":false, 
     "representation":null, 
     "open_channel_ids":null, 
     "on_hold":null, 
     "last_sla_comment":null, 
     "location":null, 
     "meta_data":{ 

     }, 
     "meter_readers":[ 

     ], 
     "collection_window":5, 
     "memo":null 
     }, 
     { 
     "_id":"5825c85d22c8aa00606b9dd5", 
     "ean":"1200004", 
     "name":null, 
     "holder":{ 
      "_id":"5543677f37c6b65f008b46e4", 
      "name":"Example account", 
      "username":"[email protected]" 
     }, 
     "root":{ 
      "_id":"577fdd960c8a335c008b466f", 
      "holder":{ 
       "_id":"577fd0170c8a335c008b45fe", 
       "name":"Rob Harvey Utility Company", 
       "entity_type":"utility_provider" 
      } 
     }, 
     "assigner":{ 
      "_id":"577fd0170c8a335c008b45fe", 
      "name":"Rob Harvey Utility Company", 
      "username":"[email protected]", 
      "manager":{ 
       "_id":null 
      } 
     }, 
     "created":"2016-11-11T13:32:13+0000", 
     "children":[ 

     ], 
     "address":"Gr\u00e4nsv\u00e4gen 12B", 
     "timezone":"Etc\/GMT-1", 
     "control_level":"shared", 
     "billing_category":"owned_meter_consumption", 
     "generation":1, 
     "tags":[ 

     ], 
     "box":"active", 
     "revoked":false, 
     "metrics":[ 
      "energy" 
     ], 
     "type":"cooling", 
     "consumption_stats":{ 
      "energy":{ 
       "month":{ 
        "count":33, 
        "sum":902350, 
        "max":92560, 
        "last":201709, 
        "min":9730, 
        "first":201501 
       }, 
       "hour":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       }, 
       "day":{ 
        "first":null, 
        "last":null, 
        "sum":null, 
        "max":null, 
        "min":null, 
        "count":0 
       } 
      } 
     }, 
     "cost_stats":{ 

     }, 
     "readings_stats":null, 
     "has_active_complaints":false, 
     "representation":null, 
     "open_channel_ids":null, 
     "on_hold":null, 
     "last_sla_comment":null, 
     "location":null, 
     "meta_data":{ 

     }, 
     "meter_readers":[ 

     ], 
     "collection_window":5, 
     "memo":null 
     } 
    ] 
} 
0

我發佈了一個關於將JSON對象展平爲鍵值p上週播出,亞歷山大彼得羅夫的迴應確實幫了我。問題是here。他的代碼將處理任何結構的JSON,因此如果更改入站對象,代碼仍然會將其轉儲出去。可能對你有用,希望它有幫助。

2

你的json在語法上不是正確的(https://jsonlint.com/),並且從我所看到的它不包含ModelClass.Rootobject的列表,而只是此類的對象。 請嘗試以下

static void Main() 
{ 
    string json2 = @"{'code':200,'message':'OK','profiling':null,'count':6,'skip':0,'limit':50,'next_page':null,'previous_page':null,'additional_meta':{'$and':[{'deleted':false},{'$or':[{'holder_id':{'$id':'5543677f37c6b65f008b46e4'}},{'root.holder._id':{'$id':'5543677f37c6b65f008b46e4'}}]}]},'data':[{'_id':'5825c6f6de22ae00632e86f3','ean':'659842','name':null,'holder':{'_id':'5543677f37c6b65f008b46e4','name':'Example account','username':'[email protected]'},'root':{'_id':'5824790e3d214b006271c515','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'[email protected]','manager':{'_id':null}},'created':'2016-11-11T13:26:14+0000','children':[],'address':'Kungsgatan 2','timezone':'Etc\/GMT-1','control_level':'shared','billing_category':'owned_meter_consumption','generation':1,'tags':[],'box':'active','revoked':false,'metrics':['energy'],'type':'heat','consumption_stats':{'energy':{'hour':{'count':26275,'sum':6546140,'max':930,'last':2016123021,'min':0,'first':2014010101},'day':{'count':1091,'sum':6522120,'max':18900,'last':20161229,'min':1180,'first':20140102},'month':{'count':33,'sum':5727310,'max':387190,'last':201708,'min':49750,'first':201402}}},'cost_stats':{},'readings_stats':null,'has_active_complaints':false,'representation':null,'open_channel_ids':null,'on_hold':null,'last_sla_comment':null,'location':null,'meta_data':{},'meter_readers':[],'collection_window':5,'memo':null},{'_id':'5825c76920fe9b006308e412','ean':'735999999564879216','name':null,'holder':{'_id':'5543677f37c6b65f008b46e4','name':'Example account','username':'[email protected]'},'root':{'_id':'5824790e3d214b006271c510','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'[email protected]','manager':{'_id':null}},'created':'2016-11-11T13:28:09+0000','children':[],'address':'Kungsgatan 2','timezone':'Etc\/GMT-1','control_level':'shared','billing_category':'owned_meter_consumption','generation':1,'tags':[],'box':'active','revoked':false,'metrics':['energy'],'type':'electricity','consumption_stats':{'energy':{'hour':{'count':25953,'sum':311714.628,'max':34.3,'last':2016121709,'min':0,'first':2014010101},'day':{'count':1080,'sum':311302.328,'max':689,'last':20161216,'min':141.1,'first':20140102},'month':{'count':35,'sum':306915.54,'max':13704.72,'last':201708,'min':6013.932,'first':201402}}},'cost_stats':{},'readings_stats':null,'has_active_complaints':false,'representation':null,'open_channel_ids':null,'on_hold':null,'last_sla_comment':null,'location':null,'meta_data':{},'meter_readers':[],'collection_window':5,'memo':null},{'_id':'5825c84b22c8aa00623d65a5','ean':'6934363','name':null,'holder':{'_id':'5543677f37c6b65f008b46e4','name':'Example account','username':'[email protected]'},'root':{'_id':'577fdd960c8a335c008b4672','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'[email protected]','manager':{'_id':null}},'created':'2016-11-11T13:31:55+0000','children':[],'address':'Gr\u00e4nsv\u00e4gen 12B','timezone':'Etc\/GMT-1','control_level':'shared','billing_category':'owned_meter_consumption','generation':1,'tags':[],'box':'active','revoked':false,'metrics':['energy'],'type':'gas','consumption_stats':{'energy':{'month':{'count':24,'sum':42656.62,'max':8890.65,'last':201708,'min':104.57,'first':201501},'hour':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0},'day':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0}}},'cost_stats':{},'readings_stats':null,'has_active_complaints':false,'representation':null,'open_channel_ids':null,'on_hold':null,'last_sla_comment':null,'location':null,'meta_data':{},'meter_readers':[],'collection_window':5,'memo':null},{'_id':'5825c85320fe9b006308e41d','ean':'5896584','name':null,'holder':{'_id':'5543677f37c6b65f008b46e4','name':'Example account','username':'[email protected]'},'root':{'_id':'577fdd960c8a335c008b466c','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'[email protected]','manager':{'_id':null}},'created':'2016-11-11T13:32:03+0000','children':[],'address':'Gr\u00e4nsv\u00e4gen 12B','timezone':'Etc\/GMT-1','control_level':'shared','billing_category':'owned_meter_consumption','generation':1,'tags':[],'box':'active','revoked':false,'metrics':['energy'],'type':'heat','consumption_stats':{'energy':{'month':{'count':57,'sum':3592320,'max':173000,'last':201709,'min':2000,'first':201301},'hour':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0},'day':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0}}},'cost_stats':{},'readings_stats':null,'has_active_complaints':false,'representation':null,'open_channel_ids':null,'on_hold':null,'last_sla_comment':null,'location':null,'meta_data':{},'meter_readers':[],'collection_window':5,'memo':null},{'_id':'5825c85920fe9b0061547253','ean':'735999666123123123','name':null,'holder':{'_id':'5543677f37c6b65f008b46e4','name':'Example account','username':'[email protected]'},'root':{'_id':'577fdd960c8a335c008b4669','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'[email protected]','manager':{'_id':null}},'created':'2016-11-11T13:32:09+0000','children':[],'address':'Gr\u00e4nsv\u00e4gen 12B','timezone':'Etc\/GMT-1','control_level':'shared','billing_category':'owned_meter_consumption','generation':1,'tags':[],'box':'active','revoked':false,'metrics':['energy'],'type':'electricity','consumption_stats':{'energy':{'month':{'count':45,'sum':141604.36,'max':6488.2,'last':201709,'min':1759.64,'first':201401},'hour':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0},'day':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0}}},'cost_stats':{},'readings_stats':null,'has_active_complaints':false,'representation':null,'open_channel_ids':null,'on_hold':null,'last_sla_comment':null,'location':null,'meta_data':{},'meter_readers':[],'collection_window':5,'memo':null},{'_id':'5825c85d22c8aa00606b9dd5','ean':'1200004','name':null,'holder':{'_id':'5543677f37c6b65f008b46e4','name':'Example account','username':'[email protected]'},'root':{'_id':'577fdd960c8a335c008b466f','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'[email protected]','manager':{'_id':null}},'created':'2016-11-11T13:32:13+0000','children':[],'address':'Gr\u00e4nsv\u00e4gen 12B','timezone':'Etc\/GMT-1','control_level':'shared','billing_category':'owned_meter_consumption','generation':1,'tags':[],'box':'active','revoked':false,'metrics':['energy'],'type':'cooling','consumption_stats':{'energy':{'month':{'count':33,'sum':902350,'max':92560,'last':201709,'min':9730,'first':201501},'hour':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0},'day':{'first':null,'last':null,'sum':null,'max':null,'min':null,'count':0}}},'cost_stats':{},'readings_stats':null,'has_active_complaints':false,'representation':null,'open_channel_ids':null,'on_hold':null,'last_sla_comment':null,'location':null,'meta_data':{},'meter_readers':[],'collection_window':5,'memo':null}]}"; 

    var items = JsonConvert.DeserializeObject<ModelsClass.Rootobject>(json2); 
    foreach (ModelsClass.Rootobject value in items) 
    { 
     Console.WriteLine(value, items); 
    } 
    Console.ReadKey(); 
}