我无法反序列化我的HTTP响应。有人可以告诉我该怎么做吗?
我最初使用List反序列化响应,但是由于响应是一个json数组,因此引发了一个空异常。
int count=0;
var obj = JsonConvert.DeserializeObject<List<getAllDevices.Class1>>(receivedData);
Console.WriteLine("\n\nReceived Data________________\n" + receivedData + "\n");
foreach (var item in obj)
{
if(item.name=="device")
Console.WriteLine(item.name);
foreach(var item2 in item)
{
if(item2.action_def.id=="increase")
count++;
}
}
我的getAllDevices类具有以下内容:
class getAllDevices
{
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
//public Class1[] Property1 { get; set; }
public class Class1
{
public string _id { get; set; }
public Discovery discovery { get; set; }
public bool is_authorized { get; set; }
public bool is_available { get; set; }
public bool is_enabled { get; set; }
public bool is_visible { get; set; }
public bool deleted { get; set; }
public string name { get; set; }
public string vendor_name { get; set; }
public string type { get; set; }
public Hardware_Id[] hardware_id { get; set; }
public DateTime created { get; set; }
public string user_id { get; set; }
public Datastream[] datastreams { get; set; }
public Trigger[] triggers { get; set; }
public Condition[] conditions { get; set; }
public Action[] actions { get; set; }
public DateTime last_updated { get; set; }
public DateTime authorization_updated_on { get; set; }
public Credentials credentials { get; set; }
public object[] preferences { get; set; }
public string[] parent_ids { get; set; }
public string classification { get; set; }
public object[] states { get; set; }
public string subtype { get; set; }
public string description { get; set; }
public Metadata metadata { get; set; }
}
public class Value
{
public bool visible { get; set; }
public string id { get; set; }
public string label { get; set; }
public string datastream_key { get; set; }
}
public class Action
{
public string _id { get; set; }
public object last_executed { get; set; }
public Action_Def action_def { get; set; }
public Param2[] _params { get; set; }
}
public class Discovery
{
public bool fake_discovery { get; set; }
}
public class Action_Def
{
public bool hidden { get; set; }
public bool show_as_manual_action { get; set; }
public string _id { get; set; }
public string id { get; set; }
public string name { get; set; }
public int order { get; set; }
public bool requires_push_message { get; set; }
public string simple_name { get; set; }
}
public class Param2
{
public Ui2 ui { get; set; }
public Value1[] values { get; set; }
public string id { get; set; }
public string label { get; set; }
public string type { get; set; }
public object default_value { get; set; }
public int limit_lower { get; set; }
public int limit_upper { get; set; }
public string values_datastream_key { get; set; }
public string values_datastream_type_id { get; set; }
public string substitution_type { get; set; }
}
public class Ui2
{
public object presentation_value { get; set; }
public string _interface { get; set; }
public int step_size { get; set; }
public int step_hold_size { get; set; }
}
public class Value1
{
public bool visible { get; set; }
public string id { get; set; }
public string label { get; set; }
public string datastream_key { get; set; }
public string _id { get; set; }
}
public class Credentials
{
public string token { get; set; }
public string refresh_token { get; set; }
public int expires_in { get; set; }
public bool connected { get; set; }
public long last_updated { get; set; }
}
public class Metadata
{
public string modelNumber { get; set; }
public string thermostatRev { get; set; }
public string runtimeRev { get; set; }
public string type { get; set; }
public string thermostat_id { get; set; }
public string id { get; set; }
public string code { get; set; }
public int house_id { get; set; }
public int dev_cat { get; set; }
public int sub_cat { get; set; }
}
public class Hardware_Id
{
public string type { get; set; }
public object id { get; set; }
}
public class Datastream
{
public string _id { get; set; }
public Current_Value current_value { get; set; }
public DateTime last_updated { get; set; }
public Datastream_Def datastream_def { get; set; }
}
public class Current_Value
{
public object value { get; set; }
public Away away { get; set; }
public Home home { get; set; }
public Sleep sleep { get; set; }
}
public class Away
{
public string name { get; set; }
public string climateRef { get; set; }
}
public class Home
{
public string name { get; set; }
public string climateRef { get; set; }
}
public class Sleep
{
public string name { get; set; }
public string climateRef { get; set; }
}
public class Datastream_Def
{
public bool log_update { get; set; }
public bool log_duplicate_update { get; set; }
public string _id { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Trigger
{
public string _id { get; set; }
public object last_triggered { get; set; }
public string datastream_id { get; set; }
public Trigger_Def trigger_def { get; set; }
public Param[] _params { get; set; }
}
public class Trigger_Def
{
public bool repeatedly_trigger { get; set; }
public bool hidden { get; set; }
public string _id { get; set; }
public string datastream_id { get; set; }
public string default_condition { get; set; }
public string default_value { get; set; }
public string id { get; set; }
public string name { get; set; }
public int order { get; set; }
public string simple_name { get; set; }
}
public class Param
{
public object[] values { get; set; }
public string id { get; set; }
public string label { get; set; }
public string type { get; set; }
public string substitution_type { get; set; }
public string default_condition { get; set; }
public object default_value { get; set; }
public string values_datastream_key { get; set; }
public Ui ui { get; set; }
public int limit_lower { get; set; }
public int limit_upper { get; set; }
}
public class Ui
{
public string _interface { get; set; }
public int step_size { get; set; }
public int step_hold_size { get; set; }
}
public class Condition
{
public string _id { get; set; }
public object last_state_change { get; set; }
public string datastream_id { get; set; }
public Condition_Def condition_def { get; set; }
public Param1[] _params { get; set; }
}
public class Condition_Def
{
public bool hidden { get; set; }
public string _id { get; set; }
public string datastream_id { get; set; }
public string default_condition { get; set; }
public string default_value { get; set; }
public string id { get; set; }
public string name { get; set; }
public int order { get; set; }
public string simple_name { get; set; }
}
public class Param1
{
public Value[] values { get; set; }
public string id { get; set; }
public string label { get; set; }
public string type { get; set; }
public string substitution_type { get; set; }
public string default_condition { get; set; }
public object default_value { get; set; }
public string values_datastream_key { get; set; }
public Ui1 ui { get; set; }
public int limit_lower { get; set; }
public int limit_upper { get; set; }
}
public class Ui1
{
public string _interface { get; set; }
public int step_size { get; set; }
public int step_hold_size { get; set; }
}
}
}
当我使用
var obj = JsonConvert.DeserializeObject<getAllDevices.Rootobject>(receivedData);
CrestronConsole.PrintLine("\n\nReceived Data________________\n" + receivedData + "\n");
foreach (var item in obj.Property1)
{
}
我得到:
Cannot deserialize JSON array into type
'yonomi_one_test_one.getAllDevices+Rootobject'.----- at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContrac (Type objectType, JsonContract contract) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String reference)
我的JSON响应如下所示。我使用JSON到c#转换工具在线获取getAllDevices类。
[ {
"_id": "5cf528969bcb5a0069cb6039",
"discovery": {
"fake_discovery": false
},
"is_authorized": true,
"name": "Plug In Module",
"conditions": [],
"actions": [
{
"_id": "5cf528969bcb5a0069cb603c",
"last_executed": "2019-06-13T14:31:56.302Z",
"action_def": {
"hidden": false,
"show_as_manual_action": true,
"_id": "5a317841ac537b0013a2a7cc",
"id": "turn_on",
"name": "Turn On to {level}%",
"order": 0,
"requires_push_message": false,
"simple_name": "Turn On to Brightness%"
},
{
"_id": "5cf528969bcb5a0069cb603e",
"last_executed": null,
"action_def": {
"hidden": false,
"show_as_manual_action": true,
"_id": "5a317899ac537b0013a2a7ce",
"id": "set_brightness",
"name": "Set Brightness to {level}%",
"order": 3,
"requires_push_message": false,
"simple_name": "Set Brightness to Level%"
},
"params": [
{
"ui": {
"interface": "slider",
"step_size": 5,
"step_hold_size": 10,
"presentation_value": 10
},
"values": [],
"id": "level",
"label": "Percent",
"type": "Number",
"default_value": 10,
"limit_lower": 0,
"limit_upper": 100
}
]
}
],
"last_updated": "2019-06-13T14:32:05.413Z",
"parent_ids": [
"5cf03e05aa4d770038167dfd"
],
"classification": "device",
"states": []
}
]
答案 0 :(得分:0)
我决定使用JArray.parse修复此问题。
parsedArray = JArray.Parse(receivedData);
foreach (JObject ob in parsedArray.Children<JObject>())
{
var name = (string)ob["name"];
if (name == "My ecobee")
{
device_id = (string)ob["_id"];
var json = (JArray)ob["actions"];
foreach (JObject ob1 in json.Children<JObject>())
{
var actionname = (string)ob1["action_def"]["id"];
if (actionname == "increase_temp_by")
{
action_id = (string)ob1["_id"];
strname = (string)ob1["params"][0]["id"];
}
}
}