简体   繁体   中英

Deserialization of complex (nested) JSON to C# objects

I have been working on a deserialization of a complex JSON response from an API. I'm new to C# and .NET so I don't really know how to continue.

What I want to do:

I want to collect all the items in the JSON-response and put them in Key-Value pairs and return them in a list in a console application for testing purpose.

It works perfectly fine when I debug and I can see that it maps all the Key-value pairs as intended. My struggle is to return everything in a list in the Console Application.

This is my code for the "Models"-class.

using System;

namespace ModelsClass
{

public class Rootobject
{
    public int code { get; set; }
    public string message { get; set; }
    public object profiling { get; set; }
    public int count { get; set; }
    public int skip { get; set; }
    public int limit { get; set; }
    public object next_page { get; set; }
    public object previous_page { get; set; }
    public Additional_Meta additional_meta { get; set; }
    public MeterData[] data { get; set; }
}

public class Additional_Meta
{
    public And[] and { get; set; }
}

public class And
{
    public bool deleted { get; set; }
    public Or[] or { get; set; }
}

public class Or
{
    public Holder_Id holder_id { get; set; }
    public RootHolder_Id rootholder_id { get; set; }
}

public class Holder_Id
{
    public string holderId { get; set; }
}

public class RootHolder_Id
{
    public string rootId { get; set; }
}

public class MeterData
{
    public string _id { get; set; }
    public string ean { get; set; }
    public object name { get; set; }
    public Holder holder { get; set; }
    public Root root { get; set; }
    public Assigner assigner { get; set; }
    public DateTime created { get; set; }
    public object[] children { get; set; }
    public string address { get; set; }
    public string timezone { get; set; }
    public string control_level { get; set; }
    public string billing_category { get; set; }
    public int generation { get; set; }
    public object[] tags { get; set; }
    public string box { get; set; }
    public bool revoked { get; set; }
    public string[] metrics { get; set; }
    public string type { get; set; }
    public Consumption_Stats consumption_stats { get; set; }
    public object readings_stats { get; set; }
    public bool has_active_complaints { get; set; }
    public object representation { get; set; }
    public object open_channel_ids { get; set; }
    public object on_hold { get; set; }
    public object last_sla_comment { get; set; }
    public object location { get; set; }
    public object[] meter_readers { get; set; }
    public int collection_window { get; set; }
    public object memo { get; set; }
}

public class Holder
{
    public string _id { get; set; }
    public string name { get; set; }
    public string username { get; set; }
}

public class Root
{
    public string _id { get; set; }
    public Holder1 holder { get; set; }
}

public class Holder1
{
    public string _id { get; set; }
    public string name { get; set; }
    public string entity_type { get; set; }
}

public class Assigner
{
    public string _id { get; set; }
    public string name { get; set; }
    public string username { get; set; }
    public Manager manager { get; set; }
}

public class Manager
{
    public object _id { get; set; }
}

public class Consumption_Stats
{
    public Energy energy { get; set; }
}

public class Energy
{
    public Hour hour { get; set; }
    public Day day { get; set; }
    public Month month { get; set; }
}

public class Hour
{
    public string count { get; set; }
    public string sum { get; set; }
    public string max { get; set; }
    public string last { get; set; }
    public string min { get; set; }
    public string first { get; set; }
}

public class Day
{
    public string count { get; set; }
    public string sum { get; set; }
    public string max { get; set; }
    public string last { get; set; }
    public string min { get; set; }
    public string first { get; set; }
}

public class Month
{
    public string count { get; set; }
    public string sum { get; set; }
    public string max { get; set; }
    public string last { get; set; }
    public string min { get; set; }
    public string first { get; set; }
}
}

And this is my code for the "WriteLine" command to try to list all Key-Value pairs in a list.

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"":""someone@example.com""},""root"":{""_id"":""5824790e3d214b006271c515"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""5824790e3d214b006271c510"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""577fdd960c8a335c008b4672"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""577fdd960c8a335c008b466c"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""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@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""577fdd960c8a335c008b466f"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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();
    }
}

So, can anyone help me with how I return all my Key-Value pairs from my Models-class to the commandline?

As you can see, the JSON-response is pretty complex with mixed objects and arrays.

Your json is not syntactically correct ( https://jsonlint.com/ ) and from what I see it doesn't contain a List of ModelClass.Rootobject but just and object of this class. Try the following

    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':'someone@example.com'},'root':{'_id':'5824790e3d214b006271c515','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'RobHarveyUtilityCompany-utility@example.com','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':'someone@example.com'},'root':{'_id':'5824790e3d214b006271c510','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'RobHarveyUtilityCompany-utility@example.com','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':'someone@example.com'},'root':{'_id':'577fdd960c8a335c008b4672','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'RobHarveyUtilityCompany-utility@example.com','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':'someone@example.com'},'root':{'_id':'577fdd960c8a335c008b466c','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'RobHarveyUtilityCompany-utility@example.com','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':'someone@example.com'},'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@example.com','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':'someone@example.com'},'root':{'_id':'577fdd960c8a335c008b466f','holder':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','entity_type':'utility_provider'}},'assigner':{'_id':'577fd0170c8a335c008b45fe','name':'Rob Harvey Utility Company','username':'RobHarveyUtilityCompany-utility@example.com','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();
}

1- You should remove $ chars from your json text

2- Root node is not array, that's why change deserialize operation like this

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

3- You can print name-value pairs with using this function.

   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- And your final main function looks like:

    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"":""someone@example.com""},""root"":{""_id"":""5824790e3d214b006271c515"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""5824790e3d214b006271c510"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""577fdd960c8a335c008b4672"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""577fdd960c8a335c008b466c"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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"":""someone@example.com""},""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@example.com"",""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"":""someone@example.com""},""root"":{""_id"":""577fdd960c8a335c008b466f"",""holder"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""entity_type"":""utility_provider""}},""assigner"":{""_id"":""577fd0170c8a335c008b45fe"",""name"":""Rob Harvey Utility Company"",""username"":""RobHarveyUtilityCompany-utility@example.com"",""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();
    }

Get the all the type & properties from your class Something like below

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();   
    }

(Made a separate post about this because of limit in characters)

If you want to see the formatted JSON example response, it is down below:

    {  
   "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":"someone@example.com"
         },
         "root":{  
            "_id":"5824790e3d214b006271c515",
            "holder":{  
               "_id":"577fd0170c8a335c008b45fe",
               "name":"Rob Harvey Utility Company",
               "entity_type":"utility_provider"
            }
         },
         "assigner":{  
            "_id":"577fd0170c8a335c008b45fe",
            "name":"Rob Harvey Utility Company",
            "username":"RobHarveyUtilityCompany-utility@example.com",
            "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":"someone@example.com"
         },
         "root":{  
            "_id":"5824790e3d214b006271c510",
            "holder":{  
               "_id":"577fd0170c8a335c008b45fe",
               "name":"Rob Harvey Utility Company",
               "entity_type":"utility_provider"
            }
         },
         "assigner":{  
            "_id":"577fd0170c8a335c008b45fe",
            "name":"Rob Harvey Utility Company",
            "username":"RobHarveyUtilityCompany-utility@example.com",
            "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":"someone@example.com"
         },
         "root":{  
            "_id":"577fdd960c8a335c008b4672",
            "holder":{  
               "_id":"577fd0170c8a335c008b45fe",
               "name":"Rob Harvey Utility Company",
               "entity_type":"utility_provider"
            }
         },
         "assigner":{  
            "_id":"577fd0170c8a335c008b45fe",
            "name":"Rob Harvey Utility Company",
            "username":"RobHarveyUtilityCompany-utility@example.com",
            "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":"someone@example.com"
         },
         "root":{  
            "_id":"577fdd960c8a335c008b466c",
            "holder":{  
               "_id":"577fd0170c8a335c008b45fe",
               "name":"Rob Harvey Utility Company",
               "entity_type":"utility_provider"
            }
         },
         "assigner":{  
            "_id":"577fd0170c8a335c008b45fe",
            "name":"Rob Harvey Utility Company",
            "username":"RobHarveyUtilityCompany-utility@example.com",
            "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":"someone@example.com"
         },
         "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@example.com",
            "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":"someone@example.com"
         },
         "root":{  
            "_id":"577fdd960c8a335c008b466f",
            "holder":{  
               "_id":"577fd0170c8a335c008b45fe",
               "name":"Rob Harvey Utility Company",
               "entity_type":"utility_provider"
            }
         },
         "assigner":{  
            "_id":"577fd0170c8a335c008b45fe",
            "name":"Rob Harvey Utility Company",
            "username":"RobHarveyUtilityCompany-utility@example.com",
            "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
      }
   ]
}

I posted a question about flattening a JSON object to key-value pairs last week, the response from Alexander Petrov really helped me out. The question is here . His code will deal with JSON of any structure, so if you change your inbound object, the code will still dump it out. Might be useful to you, hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM