在 Visual basic web 服务中使用内部数组反序列化 JSON 文本

Deserializing JSON text with inside's Array in Visual basic webservice

反序列化此 json 字符串时遇到问题:

{
"UserName": "Person1",
"TechOpinion": "Under Repair",
"CodeStatus": "0",
"DateYear": "2016",
"TechCode": "Person2",
"CReception": [{
    "CodeRecep": "146001"
}, {
    "CodeRecep": "146002"
}, {
    "CodeRecep": "146003"
}, {
    "CodeRecep": "146004"
}]
}

我想每次调用一个具有根值和一个 CodeRecep 值的存储过程(根据存储过程将调用 {{4 次此示例中的 CodeRecep 值}})
我在两个单独的 .class 文件中为 json 类型写了两个 class
第一个保持根值如下:

Public Class ArrayJSONParameter
Dim _UserName As String
Dim _TechOpinion As String
Dim _CodeStatus As String
Dim _DateYear As String
Dim _TechCode As String
Dim _CReception As CodeReceptions



Public Property UserName() As String
    Get
        Return _UserName
    End Get
    Set(ByVal value As String)
        _UserName = value
    End Set
End Property
Public Property TechOpinion() As String
    Get
        Return _TechOpinion
    End Get
    Set(ByVal value As String)
        _TechOpinion = value
    End Set
End Property
Public Property CodeStatus() As String
    Get
        Return _CodeStatus
    End Get
    Set(ByVal value As String)
        _CodeStatus = value
    End Set
End Property
Public Property DateYear() As String
    Get
        Return _DateYear
    End Get
    Set(ByVal value As String)
        _DateYear = value
    End Set
End Property
Public Property TechCode() As String
    Get
        Return _TechCode
    End Get
    Set(ByVal value As String)
        _TechCode = value
    End Set
End Property
Public Property CReception() As CodeReceptions
    Get
        Return _CReception
    End Get
    Set(ByVal value As CodeReceptions)
        _CReception = value
    End Set
End Property


End Class

第二个保存在数组中,如下所示:

Public Class CodeReceptions
Dim _CodeRece As String
Public Property CodeRecep() As String
    Get
        Return _CodeRece
    End Get
    Set(ByVal value As String)
        _CodeRece = value
    End Set
End Property
End Class

我在webservice中的主要方法代码是:

<WebMethod()> _
Public Function JsonSample(ByVal jsonTxt As String) As String
    Dim _JSONArray As ArrayJSONParameter = JsonConvert.DeserializeObject(Of ArrayJSONParameter)(jsonTxt)

    Return "sample text" '_JSONArray.UserName
End Function

但是当我尝试使用我的 json 字符串执行网络服务时,我遇到了这个错误:

    Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CodeReceptions' because the type requires a JSON object (e.g. {&quot;name&quot;:&quot;value&quot;}) to deserialize correctly.
    To fix this error either change the JSON to a JSON object (e.g. {&quot;name&quot;:&quot;value&quot;}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List&lt;T&gt; that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
    Path 'CReception', line 1, position 119.
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 823
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 848
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 293
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 1018
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 2393
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 485
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 291
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 196
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonSerializer.cs:line 823
       at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonConvert.cs:line 863
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonConvert.cs:line 820
       at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\JsonConvert.cs:line 757
       at JSONWS.JsonSample(String jsonTxt)

我花了最后两天阅读互联网上的反序列化线程,但类似的线程使用简单的 json 文本,但我的问题在数组内部。 我找不到正确的答案,所以请帮助我。 为什么我得到那个错误?以及如何在代码中访问内部数组值?

您的 CReception 属性 需要是 CodeReceptions 的数组,而不是单个 CodeReceptions:

Dim _CReception As CodeReceptions()
'...
Public Property CReception() As CodeReceptions()
    Get
        Return _CReception
    End Get
    Set(ByVal value As CodeReceptions())
        _CReception = value
    End Set
End Property

如果您愿意,它可以是 List(Of CodeReceptions)