将Json字符串反序列化为C#对象
我第一次尝试使用Philips Hue lights api,关于如何将json字符串反序列化为C#对象,我有几个问题。我正在为我正在开发的Xamarin.iOS应用程序尝试这个 以下是我的方法,可以从我周围获取灯光数据:将Json字符串反序列化为C#对象,c#,json,xamarin.ios,deserialization,C#,Json,Xamarin.ios,Deserialization,我第一次尝试使用Philips Hue lights api,关于如何将json字符串反序列化为C#对象,我有几个问题。我正在为我正在开发的Xamarin.iOS应用程序尝试这个 以下是我的方法,可以从我周围获取灯光数据: private string getLights() { var url = APIURL + APIKey + LightsEndPoint ; var request = WebRequest.Create(url); request.Cont
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;
}
}
}
我现在遇到的问题是,我的items对象总是空的,空值,即使我正确地获取了内容json字符串
我的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"
}
}
我遇到的问题是,用一个数值表示的是light,我不确定如何拆分json字符串来填充我的c#对象
基本上,我在为Xamarin.iOS应用程序的api流将json字符串转换为c#对象时遇到问题。您的模型应该是这样的
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);
反序列化调用应该是这样的
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);
JsonConvert.DeserializeObject(内容);
字典的关键是数字,值是你想要得到的灯光模型。你的模型应该是这样的
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);
反序列化调用应该是这样的
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);
JsonConvert.DeserializeObject(内容);
其中,字典的键是数字,值是要获取的灯光模型。我生成了一个类,其中包含:
公共类状态
{
{get;set;}上的公共布尔
公共整数bri{get;set;}
公共整数{get;set;}
公共整数sat{get;set;}
公共字符串效果{get;set;}
公共列表xy{get;set;}
公共int ct{get;set;}
公共字符串警报{get;set;}
公共字符串颜色模式{get;set;}
公共布尔可达{get;set;}
}
公共类根对象
{
公共状态状态{get;set;}
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串modelid{get;set;}
公共字符串制造商名称{get;set;}
公共字符串唯一标识{get;set;}
公共字符串swversion{get;set;}
}
然后我把这个代码叫做:
var a = JsonConvert.DeserializeObject<Dictionary<string, RootObject>>(json);
var a=JsonConvert.DeserializeObject(json);
结果是:
我生成了一个包含以下内容的类:
公共类状态
{
{get;set;}上的公共布尔
公共整数bri{get;set;}
公共整数{get;set;}
公共整数sat{get;set;}
公共字符串效果{get;set;}
公共列表xy{get;set;}
公共int ct{get;set;}
公共字符串警报{get;set;}
公共字符串颜色模式{get;set;}
公共布尔可达{get;set;}
}
公共类根对象
{
公共状态状态{get;set;}
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串modelid{get;set;}
公共字符串制造商名称{get;set;}
公共字符串唯一标识{get;set;}
公共字符串swversion{get;set;}
}
然后我把这个代码叫做:
var a = JsonConvert.DeserializeObject<Dictionary<string, RootObject>>(json);
var a=JsonConvert.DeserializeObject(json);
结果是:
如前所述,您的模型结构与json的不匹配。您需要相应地正确嵌套属性。我举了一个例子,虽然我不确定某些数据类型,但我只是使用了一个字符串(应该可以) 轻量化
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);
var items=JsonConvert.DeserializeObject(内容);
如前所述,您的模型结构与json的不匹配。您需要相应地正确嵌套属性。我举了一个例子,虽然我不确定某些数据类型,但我只是使用了一个字符串(应该可以)
轻量化
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);
var items=JsonConvert.DeserializeObject(内容);
您似乎要返回一个字典,请尝试反序列化到字典
。您的模型与json结构不匹配,json中存在嵌套属性,您在模型的根目录中声明这些属性。为了能够从json反序列化,您的模型需要具有与json相同的结构json@LasseV.Karlsen我在尝试以下操作时出现此错误:Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:{.Path'1.state',第1行,位置15。@RubenVardanyan如何使其与json结构匹配?我只需要在我的模型中指定的值。谢谢!@Euridice01看到下面的答案似乎您正在找回一个字典,请尝试反序列化到字典
。您的模型与json结构不匹配,我需要嵌套属性n您在模型根目录中声明的json。为了能够从json反序列化,您的模型需要与json@LasseV.Karlsen我在尝试以下操作时出现此错误:Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:{.Path'1.state',第1行,位置15。@RubenVardanyan如何使其与json结构匹配?我只需要在模型中指定的值。谢谢!@Euridice01请参见下面的答案JsonConvert.DeserializeObject(内容);
将不起作用,因为json是键和值的映射,其中值是Light
object您是对的!叮叮叮叮!我更新了它,谢谢;)JsonConvert.DeserializeObject(内容);
不起作用,因为json是键和值的映射,其中值是Light
object先生,您是对的!叮叮叮叮!我更新了它,谢谢;)