I want to read some data from the following JSON: The JSON is much longer, it is a response from the Philips Hue API to a GET requesting the list of bulbs. I only want values from the first element of the JSON WITHOUT specifying which is the first, this could vary.
{
"1":{
"modelid":"LCT001",
"name":"Hue Lamp 1",
"swversion":"65003148",
"state":{
"xy":[
0,
0
],
"ct":0,
"alert":"none",
"sat":254,
"effect":"none",
"bri":254,
"hue":4444,
"colormode":"hs",
"reachable":true,
"on":true
},
"type":"Extended color light",
"pointsymbol":{
"1":"none",
"2":"none",
"3":"none",
"4":"none",
"5":"none",
"6":"none",
"7":"none",
"8":"none"
},
"uniqueid":"00:17:88:01:00:d4:12:08-0a"
}
}
I am requesting the data up to a specific point.
string respuesta = webRequest.downloadHandler.text;
JObject root = JObject.Parse(respuesta);
JProperty property = (JProperty)root.First;
Debug.Log(property.Value["state"]);
This returns me as a response:
{
"xy": [
0,
0
],
"ct": 0,
"alert": "none",
"sat": 254,
"effect": "none",
"bri": 254,
"hue": 4444,
"colormode": "hs",
"reachable": true,
"on": true
}
But for example, is there any way to directly access the "on" element? Thank you all.
Based on your example
json
you leave and the way you manipulate it you can do the following to get the value ofon
:Another way to do it, in case you need to have the object or value of
on
separately:If you review the documentation you will notice that the indexer
property.Value["state"]
you use already returns aJToken
so you can reuse the returned token's indexer to get the value ofon
.first you must create the json model in a class, then deserialize the object and you can use the property you want. You can use http://json.parser.online.fr/ to paste the json and create the model faster.
Then you should deserialize the target
//use the library using Newtownsoft.Json;
//use this line in your method.
//the json variable is a string that you read from the service. //Then you can iterate with a foreach