2017-08-25 14 views
0

我的飛利浦色調燈泡中有一些JSON。它看起來像這樣。使用遞增整數反序列化.net中的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_8 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; } 
} 
+1

當你調試,這是什麼'model'看起來像你做你'JsonConvert.DeserializeObject'後?當你在一個字符串上調用'.Count()'時,你不會得到你要找的東西...... – ragerory

+1

你可能比使用'Dictionary 更好'作爲你的'RootObject'。您應該將ID解析爲整數,然後您將ID作爲您的關鍵字,並將它們的燈泡作爲相應的值。 – Gavin

回答

2
var dict = JsonConvert.DeserializeObject<Dictionary<string, BulbNumber>>(json); 
+0

我現在得到這個錯誤。由於類型需要JSON數組(例如[1,2,3])才能正確地反序列化,因此無法將當前JSON對象(例如{「name」:「value」})反序列化爲類型'HueWeb.Controllers.State []'「。 要修復此錯誤,請將JSON更改爲JSON數組(例如[1,2,3])或更改反序列化的類型,以使其爲正常的.NET類型(例如,不是像整數這樣的基本類型,也不是集合類型像一個數組或列表)可以從JSON對象反序列化。 JsonObjectAttribute也可以添加到類型中,以強制它從JSON對象反序列化。 – Zucchini

+1

@Zucchini * state *不是* State *的數組。將其聲明爲'public State state {get;組; }'* BulbNumber * –

+0

我怎樣才能將字典傳遞給我的觀點? – Zucchini