使用Visual Basic webservice

时间:2016-08-09 15:20:02

标签: arrays json vb.net web-services json-deserialization

反序列化此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类型编写了两个类 第一个保存根值如下:

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字符串执行web服务时,我遇到了这个错误:

    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文本,但我的问题是在数组内部。 我找不到正确的答案,所以请帮助我。 为什么我得到那个错误?以及如何在代码中访问内部数组值?

1 个答案:

答案 0 :(得分:0)

您的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)