From f4e68e9a7db6978ad2853a3cd60a7fcd480913a8 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sat, 20 Feb 2016 18:37:17 -0600 Subject: [PATCH] Resolve Issue #4 - Correct JSON Parsing based on the Property Name in the JSON instead of guessing @ the Index number --- SharpHue/Lights/LightCollection.cs | 20 +++++++------------- SharpHue/SharpHue.csproj | 5 ++--- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/SharpHue/Lights/LightCollection.cs b/SharpHue/Lights/LightCollection.cs index 540445d..bc0803a 100644 --- a/SharpHue/Lights/LightCollection.cs +++ b/SharpHue/Lights/LightCollection.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Net.Http; using Newtonsoft.Json.Linq; +using System; namespace SharpHue { @@ -69,20 +70,13 @@ public void Refresh() JObject lights = JsonClient.RequestSecure(HttpMethod.Get, "/lights") as JObject; - for (int i = 1; i <= Configuration.MAX_LIGHTS; i++) + foreach (KeyValuePair light in lights) { - if (lights[i.ToString()] != null) - { - Light l = ((JObject)lights[i.ToString()]).ToObject(); - l.ID = i; - l.RefreshState(); - Lights.Add(l); - } - else - { - // Lights are sequential, break if we don't have a light with the specified index. - break; - } + Light l = new Light(); + l = light.Value.ToObject(); + l.ID = Convert.ToInt16(light.Key); + Lights.Add(l); + l.RefreshState(); } } diff --git a/SharpHue/SharpHue.csproj b/SharpHue/SharpHue.csproj index b132b82..c4e58cf 100644 --- a/SharpHue/SharpHue.csproj +++ b/SharpHue/SharpHue.csproj @@ -100,9 +100,8 @@ - False - ..\..\Newtonsoft.Json.dll - False + ..\packages\Newtonsoft.Json.5.0.6\lib\net45\Newtonsoft.Json.dll + True