0

I have an some JSON from my Philips hue bulbs. It looks like this.

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

As you can see each bulb has a number 1, 2 ,3 etc for each bulb added to the system.

How do i create a model to deserialize this data? What i tried doesn't work.

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);
}

And my object which i mostly imported. When i try importing it directly it splits it up as 1_ 2_ 3_ etc.

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; }
}
Zucchini
  • 459
  • 6
  • 16
  • 1
    When you debug, what does `model` look like after you do your `JsonConvert.DeserializeObject`? You're not going to get what you're looking for when you call `.Count()` on a string... – ragerory Aug 25 '17 at 18:45
  • 1
    You may be better off using a `Dictionary` as your `RootObject`. You should get the IDs parsed to integers, and you'll then have the IDs as your keys with their bulbs as the respective value. – Gavin Aug 25 '17 at 18:47

1 Answers1

2
var dict = JsonConvert.DeserializeObject<Dictionary<string, BulbNumber>>(json);
L.B
  • 114,136
  • 19
  • 178
  • 224
  • I get this error now. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'HueWeb.Controllers.State[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. – Zucchini Aug 25 '17 at 19:04
  • 1
    @Zucchini *state* is not an array of *State*. declare it as `public State state { get; set; }` in *BulbNumber* – L.B Aug 25 '17 at 19:16
  • How would i then pass a dictionary to my view? – Zucchini Aug 26 '17 at 16:56