将JSON反序列化为列表(或其他对象类型)

时间:2019-07-03 16:22:15

标签: json vb.net serialization deserialization

我在将某些JSON反序列化为可以使用的对象类型时遇到问题。

我一直在尝试多种不同的方法来反序列化某些内容,但是我无法让它们中的任何一种起作用。我目前正在尝试使用Newtonsoft.Json解串器。

Public Class ServerRecord
    Inherits Record
    <Newtonsoft.Json.JsonProperty("sys_class_name")>
    Public Property sys_class_name As String
        Get
        End Get
        Set(value As String)
        End Set
    End Property

    <Newtonsoft.Json.JsonProperty("host_name")>
    Public Property host_name As String
        Get
        End Get
        Set(value As String)
        End Set
    End Property
    <Newtonsoft.Json.JsonProperty("u_recovery_time_achievable")>
    Public Property u_recovery_time_achievable As String
        Get
        End Get
        Set(value As String)
        End Set
    End Property
End Class


Dim lstSNServersList As New List(Of ServerRecord)
Dim objServiceNowTableAPIClient As TableAPI.TableAPIClient(Of ServerRecord)
Dim objServiceNowRESTQueryResponse As RESTQueryResponse(Of ServerRecord)

    objServiceNowTableAPIClient = New TableAPIClient(Of wServerRecord)(strServiceNowCMDBServersTableName, strServiceNowInstanceName, strServiceNowUser, strServiceNowPassword)
    strServiceNowQuery = "sys_class_name=cmdb_ci_win_server^ORsys_class_name=cmdb_ci_linux_server"
    objServiceNowRESTQueryResponse = objServiceNowTableAPIClient.GetByQuery(strServiceNowQuery)
    'this much does work and it does return a result set

    'this is my attempt to convert this response into a list of ServerRecords, but this does not work currently:
    lstSNServersList = JsonConvert.DeserializeObject(Of List(Of ServerRecord))(objServiceNowRESTQueryResponse.RawJSON) 

objServiceNowRestQueryResponse.RawJSON字符串如下(虽然更长):

{
   "result":[
      {
         "sys_id":"00040665dbxxxxxx96191e",
         "u_recovery_time_achievable":"720",
         "sys_class_name":"cmdb_ci_linux_server",
         "host_name":"rlserver001"
      },
      {
         "sys_id":"00ec543d1xxxx66e4bcb6d",
         "u_recovery_time_achievable":"4",
         "sys_class_name":"cmdb_ci_linux_server",
         "host_name":"plserver001"
      },
      {
         "sys_id":"0105d975dbxxxxx8961998",
         "u_recovery_time_achievable":"",
         "sys_class_name":"cmdb_ci_linux_server",
         "host_name":"tlserver001"
      }
   ]
}

这是我在尝试运行代码时收到的错误消息:

  

引发的异常:“ Newtonsoft.Json.JsonSerializationException”在   Newtonsoft.Json.dll

     

其他信息:无法反序列化当前JSON对象   (例如{“ name”:“ value”})   'System.Collections.Generic.List`1 [CMDBReconciliation.CMDBReconciliation + ServiceNowServerRecord]'   因为该类型需要JSON数组(例如[1,2,3])进行反序列化   正确地。

     

要解决此错误,请将JSON更改为JSON数组(例如   [1,2,3])或更改反序列化类型,使其成为普通的.NET   类型(例如,不是整数之类的原始类型,不是集合类型   (例如数组或列表),可以从JSON对象反序列化。   还可以将JsonObjectAttribute添加到类型中以强制其   从JSON对象反序列化。

     

路径“结果”,第1行,位置10。

1 个答案:

答案 0 :(得分:0)

这将起作用。它只是从json字符串中提取数组部分。

Dim start As Integer = objServiceNowRESTQueryResponse.IndexOf("[")
Dim last As Integer = objServiceNowRESTQueryResponse.LastIndexOf("]")
lstSNServersList = JsonConvert.DeserializeObject(Of List(Of ServerRecord))(objServiceNowRESTQueryResponse.Substring(start, last - start + 1))