使用增量整数在 .net 中反序列化 JSON
Deserializing JSON in .net with incremental integer
我的飞利浦 hue 灯泡有一些 JSON。看起来像这样。
{
"1": {
"state": {
"on": false,
"bri": 75,
"hue": 60309,
"sat": 192,
"effect": "none",
"xy": [
0.5002,
0.2691
],
"ct": 443,
"alert": "select",
"colormode": "xy",
"reachable": false
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
},
"2": {
"state": {
"on": true,
"bri": 254,
"hue": 38000,
"sat": 254,
"effect": "none",
"xy": [
0.1603,
0.3238
],
"ct": 153,
"alert": "select",
"colormode": "hs",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
},
"3": {
"state": {
"on": true,
"bri": 254,
"hue": 38000,
"sat": 254,
"effect": "none",
"xy": [
0.1603,
0.3238
],
"ct": 153,
"alert": "none",
"colormode": "hs",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
}
}
如您所见,添加到系统中的每个灯泡都有一个编号 1、2、3 等。
我如何创建一个模型来反序列化这些数据?我试过没有用。
using (var getclient = new HttpClient())
{
Rootobject model = new Rootobject();
string bulburl = "http://<ip>/api/<token>/lights";
Console.WriteLine($"URL: {bulburl}");
var json = await getclient.GetStringAsync(bulburl);
Console.WriteLine("Json count: " + json.ToString().Count());
model = JsonConvert.DeserializeObject<Rootobject>(json);
Console.WriteLine("model count: " + model.ToString().Count());
return View(model);
}
还有我主要导入的对象。当我尝试直接导入它时,它会将其拆分为 1_ 2_ 3_ 等
public class Rootobject
{
public BulbNumber[] bulbnumber { get; set; }
}
public class BulbNumber
{
public State[] state { get; set; }
public Swupdate swupdate { get; set; }
public string type { get; set; }
public string name { get; set; }
public string modelid { get; set; }
public string manufacturername { get; set; }
public string uniqueid { get; set; }
public string swversion { get; set; }
public string swconfigid { get; set; }
public string productid { get; set; }
}
public class State
{
public bool on { get; set; }
public int bri { get; set; }
public int hue { get; set; }
public int sat { get; set; }
public string effect { get; set; }
public float[] xy { get; set; }
public int ct { get; set; }
public string alert { get; set; }
public string colormode { get; set; }
public bool reachable { get; set; }
}
public class Swupdate
{
public string state { get; set; }
public object lastinstall { get; set; }
}
var dict = JsonConvert.DeserializeObject<Dictionary<string, BulbNumber>>(json);
我的飞利浦 hue 灯泡有一些 JSON。看起来像这样。
{
"1": {
"state": {
"on": false,
"bri": 75,
"hue": 60309,
"sat": 192,
"effect": "none",
"xy": [
0.5002,
0.2691
],
"ct": 443,
"alert": "select",
"colormode": "xy",
"reachable": false
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
},
"2": {
"state": {
"on": true,
"bri": 254,
"hue": 38000,
"sat": 254,
"effect": "none",
"xy": [
0.1603,
0.3238
],
"ct": 153,
"alert": "select",
"colormode": "hs",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
},
"3": {
"state": {
"on": true,
"bri": 254,
"hue": 38000,
"sat": 254,
"effect": "none",
"xy": [
0.1603,
0.3238
],
"ct": 153,
"alert": "none",
"colormode": "hs",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
}
}
如您所见,添加到系统中的每个灯泡都有一个编号 1、2、3 等。
我如何创建一个模型来反序列化这些数据?我试过没有用。
using (var getclient = new HttpClient())
{
Rootobject model = new Rootobject();
string bulburl = "http://<ip>/api/<token>/lights";
Console.WriteLine($"URL: {bulburl}");
var json = await getclient.GetStringAsync(bulburl);
Console.WriteLine("Json count: " + json.ToString().Count());
model = JsonConvert.DeserializeObject<Rootobject>(json);
Console.WriteLine("model count: " + model.ToString().Count());
return View(model);
}
还有我主要导入的对象。当我尝试直接导入它时,它会将其拆分为 1_ 2_ 3_ 等
public class Rootobject
{
public BulbNumber[] bulbnumber { get; set; }
}
public class BulbNumber
{
public State[] state { get; set; }
public Swupdate swupdate { get; set; }
public string type { get; set; }
public string name { get; set; }
public string modelid { get; set; }
public string manufacturername { get; set; }
public string uniqueid { get; set; }
public string swversion { get; set; }
public string swconfigid { get; set; }
public string productid { get; set; }
}
public class State
{
public bool on { get; set; }
public int bri { get; set; }
public int hue { get; set; }
public int sat { get; set; }
public string effect { get; set; }
public float[] xy { get; set; }
public int ct { get; set; }
public string alert { get; set; }
public string colormode { get; set; }
public bool reachable { get; set; }
}
public class Swupdate
{
public string state { get; set; }
public object lastinstall { get; set; }
}
var dict = JsonConvert.DeserializeObject<Dictionary<string, BulbNumber>>(json);