JSON.Net Deserialization error, My Class definition is wrong? - json

In VB.Net program I'm getting the "Cannot deserialize the current JSON array" when using JsonConvert.DeserializeObject(Of MCMusicElements)(sRB).
The JSON array I'm working with is:
[{"Key":465419,"MIK_Energy":3,"MIK_Camelot":"9B","MIK_BPM":118}]
If I delete the "[ ]" in the JSON string my program works. So, I'm assuming my Class definition for MCMusicElements is wrong in some way. I'd like to understand how to make it work without deleting the brackets from the JSON string.
Public Class MCMusicElements
Public Property Key As Integer
Public Property MIK_Energy As Integer
Public Property MIK_Camelot As String
Public Property MIK_BPM As Integer
End Class
Dim oResult = JsonConvert.DeserializeObject(Of MCMusicElements)(sRB)
dtDataTable.Rows(index).Item(1) = oResult.MIK_Energy
dtDataTable.Rows(index).Item(2) = oResult.MIK_Camelot
dtDataTable.Rows(index).Item(3) = oResult.MIK_BPM

you have to use list of objects, not an object
Dim oResult = JsonConvert.DeserializeObject(Of List(Of MCMusicElements))(sRB)
dtDataTable.Rows(index).Item(1) = oResult(0).MIK_Energy

Related

Deserialize JSON string (without root)to object VB.NET

I,
i need to deserialize a sting that contain JSON formatted data but without root element
the data(simplified!) are this
[{"ID":"974",
"DataIns":"2022-08-12 14:13:26",
"NumeroFattura":"CTD18473",
"DataFattura":"2022-08-08",
"RagSocMit":"Example1"},
{"ID":"973",
"DataIns":"2022-08-12 13:31:00",
"NumeroFattura":"CTCC10189",
"DataFattura":"2022-08-08",
"RagSocMit":"Example2"},
{"ID":"971",
"DataIns":"2022-08-09 15:30:29",
"NumeroFattura":"C18474",
"DataFattura":"2022-08-08",
"RagSocMit":"Example2"}]
and the class for deserializing are this
Public Class TestClass
Public Property ID As String
Public Property DataIns As String
Public Property NumeroFattura As String
Public Property DataFattura As String
Public Property RagSocMit As String
End Class
If i try to deserialize with this code
Dim result As String = ""
Dim ListaFatture As FattureAC2
...
result = reader.ReadToEnd'contain the sting json from a WS
ListaFatture = JsonConvert.DeserializeObject(Of IEnumerable(Of FattureAC2))(result)
ListaFatture are equal to NOTHING
Someone can help me?
Tnx
Salvo
you can not deserialize as IEnumerable, you have to select what do you need an array or list for example
Dim ListaFatture As List (Of TestClass)
ListaFatture = JsonConvert.DeserializeObject(Of List(Of TestClass))(result)

json key with array of values - how to parse

This is the json data I am trying to parse. (I did trim the imagedata down for example purposes)
{"imageData":["SUkqAORlAACGniG0JCHeSTV9icwWxF+N9AwzcsTDlLu+PeYCgeZXAP//","sfsdfsdyfhh2h43h8ysdfsdnvjknjfdsfdsf"]}
Any idea on how to parse it into a strongly typed class in .NET?
I am using the newtonsoft.json
I tried the following
Public Class DAFGImages
Public imageData As List(Of String)
End Class
Dim DAFGImages As List(Of DAFGImages) = Newtonsoft.Json.JsonConvert.DeserializeObject(json, GetType(List(Of DAFGImages)))
Your class already contains a List of string, so you dont need a list of DAFGImages too. I would change the class member to a property:
Public Class DAFGImages
Public Property imageData As List(Of String)
End Class
Then:
Dim jstr = ... from wherever
Dim myImgs = Newtonsoft.Json.JsonConvert.DeserializeObject(Of DAFGImages)(jstr)
myImgs.ImageData will contain the 2 elements.

VB.Net Need some tips for using JavaScriptSerializer to Deserialize JSON string

First question asked here, it is nice to be part of a coding community!
Currently I am retrieving a JSON string from an API and the response is as follows:
[{"playerId":37067559,"championId":78,"championLevel":5,"championPoints":93023,"lastPlayTime":1454133232000,"championPointsSinceLastLevel":71423,"championPointsUntilNextLevel":0},{"playerId":37067559,"championId":105,"championLevel":5,"championPoints":39025,"lastPlayTime":1454130615000,"championPointsSinceLastLevel":17425,"championPointsUntilNextLevel":0},{"playerId":37067559,"championId":81,"championLevel":5,"championPoints":37068,"lastPlayTime":1454273384000,"championPointsSinceLastLevel":15468,"championPointsUntilNextLevel":0}]
I set the string to be "response2"
Dim response2 As String = serviceRequest.DownloadString(New Uri(Mastery))
However in using the JavaScriptSerializer the code exits my console
Dim jss As New JavaScriptSerializer()
Dim model As MyModel = jss.Deserialize(Of MyModel)(response2)
Console.Write(model.championId(0))
Console.ReadLine()
I set the model for the Deserializer aswell
Public Class MyModel
Public Property playerId() As String
Public Property championId() As String
Public Property championPoints() As String
Public Property lastPlayTime() As String
Public Property championPointsSinceLastLevel() As String
Public Property championPointsUntilNextLevel() As String
End Class
However on startup the console exits itself. I'm not sure if I'm supposed to use model.championId(0) aswell, I assume to becuase there is more than one set of championId.
You need array of MyModel to hold the serialized objects
Try this (comment in code):
Dim jss As New JavaScriptSerializer()
' use model() to hold aray of MyModel
Dim model() As MyModel = jss.Deserialize(Of MyModel())(response2)
'As model is now array of MyModel you should use model(0), model(1) etc...
Console.Write(model(0).championId())

Generation of dynamic class from XML to JSON

What I am trying to achieve is convert XML to a JSON object. Currently I am doing it like this:
Public Class Person
Public Property Name As String
' other properties here'
End Class
Dim doc As XmlDocument
doc.LoadXml(arg_strXml)
Dim jsonValue As String = JsonConvert.SerializeXmlNode(doc)
Dim jsonObject = JsonConvert.DeserializeObject(Of Person)(jsonValue)
Dim firstName As String = jsonObject.Name
However the issue is the retrieved XML, and thus the deserialized JSON object has different fields/properties/elements depending on the correct function. It would be a nightmare to have a class for each possible XML.
Is there a way round not having to create a specific class (Person in this case) for each deserialize?
You can deserialze/parse your JSON string into Newtonsoft's JObject. Then you can access the properties like Dictionary(Of String, String), for example :
Dim arg_strXml = "<Person><Name>foo</Name></Person>"
Dim doc = New XmlDocument()
doc.LoadXml(arg_strXml)
Dim jsonValue = JsonConvert.SerializeXmlNode(doc)
Dim jsonObject = JObject.Parse(jsonValue)
Console.WriteLine(jsonObject("Person")("Name"))
dotnetfiddle demo
output :
foo

Google finance record Json Parsing vb.net

I am creating vb.net application. I am getting Json data from google finance. I am facing a problem in parsing. The problem is that :
I will give an example (not about google)
This is the class
Public Class MyModel
Dim m_tes As String
Dim m_client_list As String
Public Property type() As String
Get
Return m_tes
End Get
Set(ByVal value As String)
m_tes = value
End Set
End Property
Public Property client_list() As String
Get
Return m_client_list
End Get
Set(ByVal value As String)
m_client_list = value
End Set
End Property
End Class
and this is the JSON Deserializer
Dim deserializedProduct As MyModel = JsonConvert.DeserializeObject(Of MyModel)(JSON)
MsgBox(deserializedProduct.type)
MsgBox(deserializedProduct.client_list)
If I get one record Json data ,It works fine
like
dim JSON = {"type":"newModel","client_list":"Joe"}
The output of the msgbox is
newModel
Joe
The problem is that if I get a list of Json
I need a way to split this list likeh the following:
Json = {"type":"clientlist","client_list":"client 1"},{"type":"clientlist","client_list":"client 1"}
I haven't worked a whole lot with JSON, but I think you can pass a collection into the deserializer to get a back a collection of the deserialized objects.
In other words:
Dim deserializedProduct As List(Of MyModel) = JsonConvert.DeserializeObject(Of List(Of MyModel))(JSON)
The above code is similar to what you posted, except that is uses List<T> instead of a single instance of your MyModel object, and for the type used with the DeserializeObject you give it a List(Of ModelType).
Note that your MsgBox(deserializedProduct.type) and MsgBox(deserializedProduct.client_list) won't show you the results in a List<T> (you'll get the type name instead) - you'll need to loop through the list. Something like this:
For Each Dim model As MyModel In deserializedProduct
MsgBox(model.type)
MsgBox(model.client_list)
Next