将Json字符串反序列化为C#对象

Eur*_*e01 0 c# json xamarin.ios deserialization

我第一次尝试使用Philips Hue light 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; 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

光在哪里:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我现在遇到的问题是即使我正确获取了内容json字符串,我的items对象也始终为空,空值。

我的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"
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是光是由一个数值表示的,我不确定如何拆分json字符串以填充我的c#对象。

基本上,我在将json字符串转换为Xamarin.iOS应用的api流的c#对象时遇到问题。

Rub*_*yan 5

您的模型应如下所示

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; }
}
Run Code Online (Sandbox Code Playgroud)

反序列化调用应如下所示

JsonConvert.DeserializeObject<Dictionary<string, Light>>(content);
Run Code Online (Sandbox Code Playgroud)

词典的关键是数字,值是您要获取的灯光模型。