使用JSON数组进行RestSharp反序列化(Null)

时间:2015-05-09 18:30:26

标签: c# json list deserialization restsharp

我在尝试将json数组反序列化为.NET POCO对象时遇到了挑战。

这是我生成的POCO类(我使用了json2csharp):

public class ClosedBy
{
    public string link { get; set; }
    public string value { get; set; }
}

public class Location
{
    public string link { get; set; }
    public string value { get; set; }
}

public class SysDomain
{
    public string link { get; set; }
    public string value { get; set; }
}

public class AssignmentGroup
{
    public string link { get; set; }
    public string value { get; set; }
}

public class CallerId
{
    public string link { get; set; }
    public string value { get; set; }
}

public class CmdbCi
{
    public string link { get; set; }
    public string value { get; set; }
}

public class OpenedBy
{
    public string link { get; set; }
    public string value { get; set; }
}

public class Result
{
    public string skills { get; set; }
    public string u_technical_service { get; set; }
    public string urgency { get; set; }
    public string correlation_id { get; set; }
    public string u_assigned_above_tier_1 { get; set; }
    public string u_business_service { get; set; }
    public string group_list { get; set; }
    public string follow_up { get; set; }
    public string closed_at { get; set; }
    public string parent { get; set; }
    public string number { get; set; }
    public ClosedBy closed_by { get; set; }
    public string work_start { get; set; }
    public string calendar_stc { get; set; }
    public string incident_state { get; set; }
    public string category { get; set; }
    public string business_duration { get; set; }
    public string u_resolve_code { get; set; }
    public string correlation_display { get; set; }
    public string due_date { get; set; }
    public string u_first_level_resolvable { get; set; }
    public string knowledge { get; set; }
    public string made_sla { get; set; }
    public string comments_and_work_notes { get; set; }
    public string u_incident_dashboard { get; set; }
    public string user_input { get; set; }
    public string u_first_call_resolution { get; set; }
    public string rfc { get; set; }
    public string u_sla_due { get; set; }
    public string child_incidents { get; set; }
    public string u_first_assigned_to { get; set; }
    public string u_incident_model { get; set; }
    public string upon_reject { get; set; }
    public string u_set_to_p1 { get; set; }
    public string approval_history { get; set; }
    public string u_template { get; set; }
    public string work_notes { get; set; }
    public string u_reopened { get; set; }
    public string u_in_stock { get; set; }
    public string sys_id { get; set; }
    public string u_affected_user { get; set; }
    public string u_major_incident { get; set; }
    public string u_test_alex2 { get; set; }
    public string sys_created_by { get; set; }
    public string business_stc { get; set; }
    public string subcategory { get; set; }
    public string u_actual_start_date { get; set; }
    public string contact_type { get; set; }
    public string time_worked { get; set; }
    public string u_kb_article { get; set; }
    public string comments { get; set; }
    public string upon_approval { get; set; }
    public string u_resolving_company { get; set; }
    public string u_old_number { get; set; }
    public Location location { get; set; }
    public string expected_start { get; set; }
    public string reopen_count { get; set; }
    public string u_schedule { get; set; }
    public string close_notes { get; set; }
    public string u_asset_redeployable { get; set; }
    public string service_offering { get; set; }
    public string impact { get; set; }
    public string priority { get; set; }
    public string description { get; set; }
    public SysDomain sys_domain { get; set; }
    public string u_on_hold_type { get; set; }
    public string sys_mod_count { get; set; }
    public string work_notes_list { get; set; }
    public string u_actual_end_date { get; set; }
    public string u_cancelled_sub_state { get; set; }
    public string sla_due { get; set; }
    public string u_priority_down { get; set; }
    public string sys_updated_on { get; set; }
    public string u_critical_outage_communicatio { get; set; }
    public string work_end { get; set; }
    public string u_protocol_not_followed { get; set; }
    public string activity_due { get; set; }
    public string company { get; set; }
    public string active { get; set; }
    public string u_created_by_tier_1 { get; set; }
    public string u_sub_state { get; set; }
    public AssignmentGroup assignment_group { get; set; }
    public CallerId caller_id { get; set; }
    public string parent_incident { get; set; }
    public string state { get; set; }
    public string approval_set { get; set; }
    public string sys_created_on { get; set; }
    public string reassignment_count { get; set; }
    public string u_lateral_assignment { get; set; }
    public string u_attached_knowledge { get; set; }
    public string u_symptom { get; set; }
    public string opened_at { get; set; }
    public string order { get; set; }
    public string short_description { get; set; }
    public string u_incident_state_count { get; set; }
    public string resolved_by { get; set; }
    public string sys_updated_by { get; set; }
    public string notify { get; set; }
    public string u_priority_up { get; set; }
    public string problem_id { get; set; }
    public string calendar_duration { get; set; }
    public string close_code { get; set; }
    public string approval { get; set; }
    public string caused_by { get; set; }
    public string severity { get; set; }
    public string u_kb_solution_steps { get; set; }
    public string resolved_at { get; set; }
    public string assigned_to { get; set; }
    public string u_sla_met { get; set; }
    public CmdbCi cmdb_ci { get; set; }
    public OpenedBy opened_by { get; set; }
    public string sys_class_name { get; set; }
    public string u_first_line_resolved { get; set; }
    public string watch_list { get; set; }
    public string escalation { get; set; }
    public string u_device_asset_name { get; set; }
    public string u_open_url { get; set; }
}

public class RootObjectList
{
    public List<Result> result { get; set; }
}

这是json的结果:

{"result":[{"skills":"","u_technical_service":"","urgency":"1","correlation_id":"","u_assigned_above_tier_1":"false","u_business_service":"","group_list":"","follow_up":"","closed_at":"2015-02-17 23:43:26","parent":"","number":"INC0000101","closed_by":{"link":"https://some url","value":"6816f79cc0a8016401c5a33be04be441"},"work_start":"","calendar_stc":"5283743","incident_state":"7","category":"inquiry","business_duration":"1970-01-15 14:43:26","u_resolve_code":"","correlation_display":"","due_date":"","u_first_level_resolvable":"false","knowledge":"false","made_sla":"false","comments_and_work_notes":"","u_incident_dashboard":"false","user_input":"","u_first_call_resolution":"false","rfc":"","u_sla_due":"","child_incidents":"","u_first_assigned_to":"","u_incident_model":"","upon_reject":"cancel","u_set_to_p1":"","approval_history":"","u_template":"","work_notes":"","u_reopened":"false","u_in_stock":"","sys_id":"ccefe8620a0006d400b6b6e1beec71d7","u_affected_user":"","u_major_incident":"false","u_test_alex2":"","sys_created_by":"admin","business_stc":"1262606","subcategory":"","u_actual_start_date":"","contact_type":"phone","time_worked":"","u_kb_article":"","comments":"","upon_approval":"proceed","u_resolving_company":"","u_old_number":"","location":{"link":"someurl","value":"db9a923c0a0a0a6501068d6eaec25ee0"},"expected_start":"","reopen_count":"","u_schedule":"","close_notes":"Done","u_asset_redeployable":"","service_offering":"","impact":"1","priority":"2","description":"","sys_domain":{"link":"https:someurl","value":"global"},"u_on_hold_type":"","sys_mod_count":"9","work_notes_list":"","u_actual_end_date":"","u_cancelled_sub_state":"","sla_due":"","u_priority_down":"","sys_updated_on":"2015-02-18 00:16:58","u_critical_outage_communicatio":"","work_end":"","u_protocol_not_followed":"false","activity_due":"","company":"","active":"false","u_created_by_tier_1":"false","u_sub_state":"1","assignment_group":{"link":"someurl","value":"d625dccec0a8016700a222a0f7900d06"},"caller_id":{"link":"someurl","value":"9ee1b13dc6112271007f9d0efdb69cd0"},"parent_incident":"","state":"7","approval_set":"","sys_created_on":"2014-12-18 20:02:23","reassignment_count":"0","u_lateral_assignment":"false","u_attached_knowledge":"","u_symptom":"","opened_at":"2014-12-18 20:01:03","order":"","short_description":"Bond Trading app is not available","u_incident_state_count":"","resolved_by":"","sys_updated_by":"Timothy_Woodruff","notify":"1","u_priority_up":"","problem_id":"","calendar_duration":"1970-03-03 03:42:23","close_code":"Solved (Work Around)","approval":"not requested","caused_by":"","severity":"3","u_kb_solution_steps":"","resolved_at":"","assigned_to":"","u_sla_met":"","cmdb_ci":{"link":"someurl","value":"250b0f530a0a0b1d00b60acc42ff36a6"},"opened_by":{"link":"someurl","value":"6816f79cc0a8016401c5a33be04be441"},"sys_class_name":"incident","u_first_line_resolved":"false","watch_list":"","escalation":"0","u_device_asset_name":"","u_open_url":""},{"skills":"","u_technical_service":"","urgency":"1","correlation_id":"","u_assigned_above_tier_1":"false","u_business_service":"","group_list":"","follow_up":"","closed_at":"2015-02-17 23:43:27","parent":"","number":"INC0000102","closed_by":{"link":"someurl","value":"6816f79cc0a8016401c5a33be04be441"},"work_start":"","calendar_stc":"3362","incident_state":"7","category":"inquiry","business_duration":"1970-01-01 00:56:02","u_resolve_code":"","correlation_display":"","due_date":"","u_first_level_resolvable":"false","knowledge":"false","made_sla":"true","comments_and_work_notes":"","u_incident_dashboard":"false","user_input":"","u_first_call_resolution":"false","rfc":"","u_sla_due":"","child_incidents":"","u_first_assigned_to":"","u_incident_model":"","upon_reject":"cancel","u_set_to_p1":"","approval_history":"","u_template":"","work_notes":"","u_reopened":"false","u_in_stock":"","sys_id":"cd5149720a0006d400a0bc76eccf54cc","u_affected_user":"","u_major_incident":"false","u_test_alex2":"","sys_created_by":"admin","business_stc":"3362","subcategory":"","u_actual_start_date":"","contact_type":"phone","time_worked":"","u_kb_article":"","comments":"","upon_approval":"proceed","u_resolving_company":"","u_old_number":"","location":{"link":"someurl","value":"db9a923c0a0a0a6501068d6eaec25ee0"},"expected_start":"","reopen_count":"","u_schedule":"","close_notes":"Done","u_asset_redeployable":"","service_offering":"","impact":"1","priority":"1","description":"","sys_domain":{"link":"someurl","value":"global"},"u_on_hold_type":"","sys_mod_count":"4","work_notes_list":"","u_actual_end_date":"","u_cancelled_sub_state":"","sla_due":"","u_priority_down":"","sys_updated_on":"2015-02-17 23:43:27","u_critical_outage_communicatio":"","work_end":"","u_protocol_not_followed":"false","activity_due":"2015-02-18 00:49:35","company":"","active":"false","u_created_by_tier_1":"false","u_sub_state":"","assignment_group":{"link":"someurl","value":"d625dccec0a8016700a222a0f7900d06"},"caller_id":{"link":"someurl","value":"9ee1b13dc6112271007f9d0efdb69cd0"},"parent_incident":"","state":"7","approval_set":"","sys_created_on":"2015-02-17 22:48:01","reassignment_count":"0","u_lateral_assignment":"false","u_attached_knowledge":"","u_symptom":"","opened_at":"2015-02-17 22:47:25","order":"","short_description":"App is not visible","u_incident_state_count":"","resolved_by":"","sys_updated_by":"admin","notify":"1","u_priority_up":"","problem_id":"","calendar_duration":"1970-01-01 00:56:02","close_code":"Solved (Work Around)","approval":"not requested","caused_by":"","severity":"3","u_kb_solution_steps":"","resolved_at":"","assigned_to":"","u_sla_met":"","cmdb_ci":{"link":"https://somerul","value":"250b0f530a0a0b1d00b60acc42ff36a6"},"opened_by":{"link":"someurl","value":"6816f79cc0a8016401c5a33be04be441"},"sys_class_name":"incident","u_first_line_resolved":"false","watch_list":"","escalation":"0","u_device_asset_name":"","u_open_url":""}]}

以下是我如何调用RestSharp:

var request = new RestRequest("api/now/table/incident", Method.GET) { RequestFormat = DataFormat.Json };

var response = client.Execute<RootObjectList>(request);

if (response.Data == null)
   throw new Exception(response.ErrorMessage);

我得到的ErrorMessage是:

  

无法转换类型为&#39; System.String&#39;的对象键入&#39; System.Collections.Generic.IDictionary`2 [System.String,System.Object]&#39;。

我也尝试过阅读其他几个stackoverflow帖子,但我没有成功。如果有人能提供帮助,我们将非常感激! :)祝你有个美好的一天!

0 个答案:

没有答案