0

I have a query response from an API looking like this:

{  
   "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
      }
   ]
}

What I want to do is to evaluate what kind of metrics there is (it can be accessible with using json.data[r].readings_stats[0] after my deserialization.

I get a list of aprox 1000 meters with this format. So the [r] after data represents different meters and [0] in readings_stats represents the first string in the metrics object.

How to achieve this?

I want to pass data like this:

If readings_stats contains energy in meter [0] or [1]; Run a function that saves json.data[0].readings_stats.energy.last_value to a variable called Energy_[0].

If readings_stats contains flow in meter [0] or [1]; Run a function that saves json.data[0].readings_stats.energy.last_value to a variable called Flow_[0].

If readings_stats is "null"; iterate the index of data[r] and continue to evaluate next data table.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ka-hn
  • 65
  • 6
  • Wouldnt it be easier to serialize the json data in classes? Something like this: http://json2csharp.com/ can help – EpicKip Oct 10 '17 at 09:58
  • I have it all in classes but how do i access them? dynamic data = JsonConvert.DeserializeObject(json) is what I'm using and it is deserializing all items inte object but how to I for instance map one value from there to a variable then? – ka-hn Oct 10 '17 at 10:01
  • If you have it in classes its not dynamic, I mean your own classes... look at this: https://stackoverflow.com/questions/25052293/deserialize-json-to-c-sharp-classes then use to classes generated by: http://json2csharp.com/ – EpicKip Oct 10 '17 at 10:04

0 Answers0