Ahmad Karrabi Ahmad Karrabi - 3 months ago 16
JSON Question

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

I have a problem when Deserializing this json string:

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


I want to call an stored procedure with root values and one of CodeRecep values each time(by number of CodeRecep values the stored procedure will call {{4 times in this sample}} )

I write two class for json type in two seperate .class files

first one to hold root values as follow:

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


and the second one to hold inside array as follow:

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


my main method code in webservice is :

<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


but when i'm try to execute the webservice with my json string i face this error:

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)


i spend last two days to read Deserializing threads in internet but similar threads use simple json text but my problem is inside's array.
i cant find the right answer so please help me on that.
why i get that error? and beside how can i access inside array values in code?

Answer

Your CReception property needs to be an array of CodeReceptions, not a single 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

It could be a List(Of CodeReceptions) instead, if you prefer.