I am trying to convert this JSON to objects with certain existing attributes in JSON, currently I am receiving this JSON as a string. I have been able to count the number of existing elements or query a single attribute, but I cannot find a way to create an object without knowing the exact name of the parent. This JSON is a response from the Philips Hue API.
"3":{
"state":{
"on":true,
"bri":254,
"hue":27306,
"sat":140,
"effect":"none",
"xy":[
0.2637,
0.5264
],
"ct":153,
"alert":"select",
"colormode":"hs",
"mode":"homeautomation",
"reachable":false
},
"swupdate":{
"state":"noupdates",
"lastinstall":"2020-01-30T12:22:09"
},
"type":"Extended color light",
"name":"Hue color lamp 1",
"modelid":"LCT015",
"manufacturername":"Signify Netherlands B.V.",
"productname":"Hue color lamp",
"capabilities":{
"certified":true,
"control":{
"mindimlevel":1000,
"maxlumen":806,
"colorgamuttype":"C",
"colorgamut":[
[
0.6915,
0.3083
],
[
0.17,
0.7
],
[
0.1532,
0.0475
]
],
"ct":{
"min":153,
"max":500
}
},
"streaming":{
"renderer":true,
"proxy":true
}
},
"config":{
"archetype":"sultanbulb",
"function":"mixed",
"direction":"omnidirectional",
"startup":{
"mode":"safety",
"configured":true
}
},
"uniqueid":"00:17:88:01:04:0c:5b:8b-0b",
"swversion":"1.46.13_r26312",
"swconfigid":"52E3234B",
"productid":"Philips-LCT015-1-A19ECLv5"
},
"5":{
"state":{
"on":true,
"bri":236,
"alert":"select",
"mode":"homeautomation",
"reachable":false
},
"swupdate":{
"state":"noupdates",
"lastinstall":"2020-02-21T12:27:34"
},
"type":"Dimmable light",
"name":"Hue white lamp 1",
"modelid":"LWB010",
"manufacturername":"Signify Netherlands B.V.",
"productname":"Hue white lamp",
"capabilities":{
"certified":true,
"control":{
"mindimlevel":2000,
"maxlumen":806
},
"streaming":{
"renderer":false,
"proxy":false
}
},
"config":{
"archetype":"classicbulb",
"function":"functional",
"direction":"omnidirectional",
"startup":{
"mode":"safety",
"configured":true
}
},
"uniqueid":"00:17:88:01:04:a5:ab:fc-0b",
"swversion":"1.46.13_r26312",
"swconfigid":"322BB2EC",
"productid":"Philips-LWB010-1-A19DLv4"
}
}
The names of the Parents ("3") and ("5") are automatically generated, I cannot create an object by setting a specific name as parent, it varies. Yes, they are always numbers.
My idea is to create objects with these attributes:
public class Light
{
public string lightNumber { get; set; }
public string lightOn { get; set; }
public string lightHue { get; set; }
public string lightBri { get; set; }
}
I would like to generate as many objects as there are parent elements in the JSON and store the data on, bri, hue and the number of the parent as attributes of the object.
I am using C# and the Newtonsoft.Json library.
when you have those dynamic values (the "3" and the "5"), you can deserialize it via
Dictionary<string, MyClass>
. I give you an example of a console application which reads a filedata.json
with the content you have shown and then deserializes it. I have generated the classes corresponding to your file to be able to deserialize.First of all I think you have the JSON in bad format, in the API try with a
Then you have to generate a class from that JSON, to do so put in google json to "the language you use". Once you have the class created (I am going to put the example in C#) you have to download and import Newtonsoft. Then you take your string with the JSON and deserialize it. RootObject is the automatically generated class.
After that the obj variable is your JSON in object format.