Removing json array deserialization in .net class

I'm having problems deserializing json data, getting InvalidCastExceptions, etc.

Can someone point me in the right direction?

Here json I want to deserialize;

[{"OrderId": 0, "Name": "Summary", "MAXLEN": "200"}, {"OrderId": 1, "Name": "Details", "MaxLen": "0"}]

Here is my code;

Public Class jsTextArea Public OrderId As Integer Public Name As String Public MaxLen As String End Class Dim js As New System.Web.Script.Serialization.JavaScriptSerializer Dim rawdata = js.DeserializeObject(textAreaJson) Dim lstTextAreas As List(Of jsTextArea) = CType(rawdata, List(Of jsTextArea)) 
+4
source share
4 answers

OrderId is an Int in your json (note the lack of fo for quotes around the values), but you declare it as a String in "jsTextArea". Also, if the type returned by rawdata does not have the value specified in the list (from jsTextArea), which probably does not show the code you showed, will not work.

Update To get data from a list (from jsTextArea), try the following:

  Dim js As New System.Web.Script.Serialization.JavaScriptSerializer Dim lstTextAreas = js.Deserialize(Of List(Of jsTextArea))(textAreaJson) 
+5
source

Doing all of this on one line helped:

 Dim lstTextAreas As List(Of jsTextArea) = js.Deserialize(textAreaJson, GetType(List(Of jsTextArea))) 
+2
source
 Dim textAreaJson As String = "[{""OrderId"":0,""Name"":""Summary"",""MaxLen"":""200""},{""OrderId"":1,""Name"":""Details"",""MaxLen"":""0""}]" Dim js As New System.Web.Script.Serialization.JavaScriptSerializer Dim lstTextAreas As jsTextArea() = js.Deserialize(Of jsTextArea())(textAreaJson) 
0
source

Here's the JSON deserialization function of any type:

  Public Function DeserializeJson(Of T)(json As String) As T Return New JavaScriptSerializer().Deserialize(Of T)(json) End Function 
0
source

Source: https://habr.com/ru/post/1301424/


All Articles