我第一次尝试飞利浦Hue灯api,我有一些关于如何将json字符串反序列化为C#对象的问题。我正在尝试使用我正在处理的Xamarin.iOS应用程序。
这是我的方法,可以从我周围获取光数据:
private string getLights()
{
var url = APIURL + APIKey + LightsEndPoint ;
var request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "GET";
using (var response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
Console.Out.WriteLine(
"Error fetching data. Server returned status code: {0}",
response.StatusCode);
using (var reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
if(string.IsNullOrWhiteSpace(content))
{
Console.WriteLine("Response contained empty body...");
}
else
{
Console.WriteLine("Response Body: \r\n {0}", content);
var items = JsonConvert.DeserializeObject <Light> (content);
}
return content;
}
}
}
光明在哪里:
public class Light
{
public Light()
{ }
public string LightName { get; set;}
[JsonProperty("state")]
public string IsOn { get; set; }
[JsonProperty("sat")]
public int Saturation { get; set; }
[JsonProperty("bri")]
public int Brightness {get;set;}
[JsonProperty("hue")]
public int Hue { get; set; }
}
我现在遇到的问题是我的items对象总是空的,即使我正确地获取内容json字符串也是null值。
我的json字符串看起来像这样:
{
"1":{
"state":{
"on":true,
"bri":254,
"hue":20000,
"sat":100,
"effect":"none",
"xy":[
0.4146,
0.4155
],
"ct":299,
"alert":"none",
"colormode":"hs",
"reachable":true
},
"type":"Extended color light",
"name":"Hue color lamp 1",
"modelid":"LCT007",
"manufacturername":"Philips",
"uniqueid":"00:17:88:01:10:26:3f:12-0b",
"swversion":"5.38.1.14919"
},
"2":{
"state":{
"on":false,
"bri":254,
"hue":50000,
"sat":254,
"effect":"none",
"xy":[
0.2468,
0.0843
],
"ct":153,
"alert":"none",
"colormode":"hs",
"reachable":true
},
"type":"Extended color light",
"name":"Hue color lamp 2",
"modelid":"LCT007",
"manufacturername":"Philips",
"uniqueid":"00:17:88:01:10:5d:fd:f6-0b",
"swversion":"5.38.1.14919"
},
"3":{
"state":{
"on":true,
"bri":254,
"hue":10000,
"sat":254,
"effect":"none",
"xy":[
0.5711,
0.3986
],
"ct":500,
"alert":"none",
"colormode":"hs",
"reachable":true
},
"type":"Extended color light",
"name":"Hue color lamp 3",
"modelid":"LCT007",
"manufacturername":"Philips",
"uniqueid":"00:17:88:01:10:26:3d:17-0b",
"swversion":"5.38.1.14919"
}
}
我遇到的问题是光线由数值表示,我不知道如何拆分json字符串来填充我的c#对象。
基本上,我在将Xson字符串转换为适用于Xamarin.iOS app的api流的c#对象时遇到问题。
答案 0 :(得分:4)
您的模型应如下所示
public class Light
{
public Light()
{
}
[JsonProperty("name")]
public string LightName { get; set;}
[JsonProperty("state")]
public State State { get; set; }
}
public class State
{
public State()
{
}
[JsonProperty("on")]
public bool IsOn { get; set; }
[JsonProperty("sat")]
public int Saturation { get; set; }
[JsonProperty("bri")]
public int Brightness {get;set;}
[JsonProperty("hue")]
public int Hue { get; set; }
}
反序列化调用看起来应该是这样的
JsonConvert.DeserializeObject<Dictionary<string, Light>>(content);
字典的键是数字,而值是你想要的光模型。
答案 1 :(得分:1)
我使用json2csharp生成了一个类:
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 List<double> 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 RootObject
{
public State state { 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; }
}
然后我调用了这段代码:
var a = JsonConvert.DeserializeObject<Dictionary<string, RootObject>>(json);
结果如下:
答案 2 :(得分:1)
如上所述,您的模型结构与json的模型结构不匹配。您需要相应地正确嵌套属性。我把一个例子汇总在一起,虽然某些数据类型我不确定我只是使用了一个字符串(应该没问题)。
Light.cs
public class Light
{
public string LightName { get; set; }
[JsonProperty("state")]
public State LightState { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("modelid")]
public string ModelId { get; set; }
[JsonProperty("manufacturername")]
public string Manufacturer { get; set; }
[JsonProperty("uniqueid")]
public string UniqueId { get; set; }
[JsonProperty("swversion")]
public string SwVersion { get; set; }
}
State.cs
public class State
{
[JsonProperty("on")]
public bool IsOn { get; set; }
[JsonProperty("bri")]
public int Brightness { get; set; }
[JsonProperty("hue")]
public int Hue { get; set; }
[JsonProperty("sat")]
public int Saturation { get; set; }
[JsonProperty("effect")]
public string Effect { get; set; } // Just making it a string for now
[JsonProperty("xy")]
public double[] XY { get; set; }
[JsonProperty("ct")]
public int CT { get; set; }
[JsonProperty("alert")]
public string Alert { get; set; } // Just making another string for now
[JsonProperty("colormode")]
public string ColorMode { get; set; } // Hey, it's another string for now
[JsonProperty("reachable")]
public bool Reachable { get; set; }
}
然后反序列化:
var items = JsonConvert.DeserializeObject<Dictionary<string, Light>> (content);