【发布时间】:2019-10-28 08:33:00
【问题描述】:
我无法反序列化我的 HTTP 响应。谁能告诉我具体怎么做?
我最初使用 List 来反序列化响应,但由于响应是一个 json 数组,所以它抛出了一个 null 异常。
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": []
}
]
【问题讨论】:
-
看起来你应该反序列化为
getAllDevices.Rootobject,而不是Class1。如果您也发布 JSON 响应会有所帮助,但我猜您会得到一组对象,而不是单个Class1... 然后您需要将您的for循环修改为foreach (var item in obj.Property1) { ... } -
@RonBeyer 我确实尝试过并获得了上述异常。
-
请提供您收到的 JSON 响应示例。
-
@RossBush 他们是否在单个编译程序集中使用它真的没关系。未标记为
public的类自动为internal,对程序集公开。这不是这里的问题。 -
@V_stack 您的 JSON 响应字符串格式不正确。能否请您发布正确的 JSON 响应字符串?
标签: c# json http deserialization