I tried to deserialize the following Json:
{
"Compensations":
[
{"Name": "Compensation_01"},
{"Name": "Compensation_02"}
]
}
paste special => paste json as classes results in
Public Class Rootobject
Public Property Compensations() As Compensation
End Class
Public Class Compensation
Public Property Name As String
End Class
both NewtonSoft:
Dim cmpnstn = JsonConvert.DeserializeObject(Of Rootobject)(json)
and Microsoft:
Dim cmpnstn = JsonSerializer.Deserialize(Of Rootobject)(json)
throw exceptions:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ...
System.Text.Json.JsonException: 'The JSON value could not be converted to Path: ... LineNumber: ... BytePositionInLine:
Public Property Compensations() As Compensation result in the IL Code (no array)
public Compensation Compensations
{
get;
set;
}
When I change the property to
Public Property Compensations As Compensation()
I get the IL code for an array
public Compensation[] Compensations
{
get;
set;
}
My question is: Why does that paste special do that form of property declaration
and what is the meaning of that Compensations() As Compensation.
change
Public Property Compensations() As Compensation
to
Public Property Compensations As Compensation()
The latter solves the problem.
Related
I am trying to convert a JSON returned from a third party API, into VB.NET class object. The problem is that the JSON sometimes returns a node as an Array of objects and sometimes the same node as an single Object. So, while trying to convert when it receives Array of objects, the code throws exception.
{
"correlation_id": "228.9219622269229",
"Error": {
"messages": [
{
"code": "401",
"description": "Unauthorized"
}
]
},
"transaction_status": "Not Processed"
}
As you can see, there is an array of Error messages under Error->messages node. But sometimes, the JSON returned is simpy and object of Error messages something like
{
"correlation_id": "228.9219622269229",
"Error": {
"messages": {
"code": "401",
"description": "Unauthorized"
}
},
"transaction_status": "Not Processed"
}
I try to deserialize this JSON to the following class
Public Class PayeezyRefundResponse
Public correlation_id As String
Public transaction_status As String
Public validation_status As String
Public transaction_type As String
Public transaction_id As String
Public transaction_tag As String
Public bank_resp_code As String
Public bank_message As String
Public [Error] As PayeezyError
End Class
Public Class PayeezyError
Public messages As PayeezyErrorMessages()
End Class
Public Class PayeezyErrorMessages
Public code As String
Public description As String
End Class
But when the JSON returns Error message as single object, the code throws exception on PayeezyError class. How can I convert the JSON to this class so that it works in both the cases (i.e. with Array of objects and with single Object) ?
You can build a custom converter that can handle both cases as a List(Of PayeezyErrorMessage).
This converter always returns a List(Of PayeezyErrorMessage), even when the JSON contains a single object.
Note: in code, PayeezyErrorMessages, plural, has been renamed to PayeezyErrorMessage, single, since this class generates a single object.
The custom converter is added to the property as an attribute:
<JsonConverter(GetType(PayeezyErrorsConverter(Of PayeezyErrorMessage)))>
Public Messages As List(Of PayeezyErrorMessage)
Refactored code:
Public Class PayeezyRefundResponse
' [...]
<JsonProperty("Error")>
Public Errors As PayeezyErrors
End Class
Public Class PayeezyErrors
<JsonProperty("messages")>
<JsonConverter(GetType(PayeezyErrorsConverter(Of PayeezyErrorMessage)))>
Public Messages As List(Of PayeezyErrorMessage)
End Class
Public Class PayeezyErrorMessage
Public code As String
Public description As String
End Class
Custom converter:
► The writer part is not implemented, since you probably won't need to send back this JSON
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class PayeezyErrorsConverter(Of T)
Inherits JsonConverter
Public Overrides Function CanConvert(objectType As Type) As Boolean
Return (objectType = GetType(List(Of T)))
End Function
Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object
Dim token As JToken = JToken.Load(reader)
If token.Type = JTokenType.Array Then
Return token.ToObject(Of List(Of T))()
End If
Return New List(Of T)() From {
token.ToObject(Of T)()
}
End Function
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return False
End Get
End Property
Public Overrides Sub WriteJson(writer As JsonWriter, value As Object, serializer As JsonSerializer)
Throw New NotImplementedException()
End Sub
End Class
My class is not being populated with the JSON string
Using the VS 2012 Edit -> Paste Special -> JSON as a CLASS
The Class Created:
Public Class clsMCParcelData
Public Class Rootobject
Public Property ParcelFound As Boolean
Public Property ParcelInfo As New APN
End Class
Public Class APN
Public Property Book As String
Public Property Map As String
Public Property Item As String
Public Property clean As String
Public Property formatted As String
Public Property link As String
End Class
END CLASS
JSON String:
{
"ParcelFound":true,
"APN":{
"Book":"510",
"Map":"11",
"Item":"668",
"clean":"51011668",
"formatted":"510-11-668",
"link":"\/api\/parcel\/51011668"
}
}
Code on the asp.vb page:
Dim jss As New JavaScriptSerializer, MCD As New clsMCParcelData.Rootobject
MCD = jss.Deserialize(Of clsMCParcelData.Rootobject)(raw)
I would expect that once deserialized, I would be able to access the APN information like so:
tbParcelNumber.text = MCD.ParcelInfo.formatted (returning 510-11-668)
What I get is Nothing as the value of MCD.ParcelInfo.formatted
The JavaScriptSerializer requires an exact match in the class to deserialize.
Below is a json file that needs to be deserialized and to be stored in different variables.
With this current json, which is returned by an API, I am unable to deserialize because it gives me and error -
Please help me on deserializing this particular json
I have added the code i used, but it returns wrong values and null reference.
{
"result": {
"candidates": [
{
"similarity": 0.1330482513,
"person_id": "75741ea3-4d9b-4e25-8460-16444ee39946",
"descriptor_id": "2f228007-350e-4d58-9897-4b62e9978081",
"user_data": "Без названия (9)",
"external_id": null
}
],
"face": {
"id": "a1b224a3-60c6-4733-9bbc-136d53ea011c",
"score": 0.9320185781
}
},
"timestamp": 1569957900.1488559,
"source": "search",
"event_type": "match",
"authorization": {
"token_id": "71f9b3e0-51b1-480f-93b9-0e76e260bcbc",
"token_data": "first token"
},
"template": {
"descriptor_id": "a1b224a3-60c6-4733-9bbc-136d53ea011c"
},
"candidate": {
"list_id": "6e64e600-cd77-4894-940e-6f7022d8aba8",
"list_data": "FaceStream_search_list(DON'T DELETE)",
"list_type": 1
}
}
I have tried :
Public Class Rootobject
Public Property result As Result
Public Property timestamp As Single
Public Property source As String
Public Property event_type As String
Public Property authorization As Authorization
Public Property template As Template
Public Property candidate As Candidate1
End Class
Public Class Result
Public Property candidates() As Candidate
Public Property face As Face
End Class
Public Class Face
Public Property id As String
Public Property score As Single
End Class
Public Class Candidate
Public Property similarity As Double
Public Property person_id As String
Public Property descriptor_id As String
Public Property user_data As String
Public Property external_id As Object
End Class
Public Class Authorization
Public Property token_id As String
Public Property token_data As String
End Class
Public Class template
Public Property descriptor_id As String
End Class
Public Class Candidate1
Public Property list_id As String
Public Property list_data As String
Public Property list_type As Integer
End Class
''And used
Dim Candidatess As New Candidate
Candidatess = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Candidate)((JObject.Parse(e.Message)).ToString)
msg(Candidatess.similarity.ToString)
msg(Candidatess.descriptor_id.ToString)
msg(Candidatess.person_id.ToString)
''REturns me Null
Public Class Candidate
Public Property similarity As Double
Public Property person_id As String
Public Property descriptor_id As String
Public Property user_data As String
Public Property external_id As Object
End Class
Public Class Face
Public Property id As String
Public Property score As Double
End Class
Public Class Result
Public Property candidates As Candidate()
Public Property face As Face
End Class
Public Class Authorization
Public Property token_id As String
Public Property token_data As String
End Class
Public Class Template
Public Property descriptor_id As String
End Class
Public Class Candidate
Public Property list_id As String
Public Property list_data As String
Public Property list_type As Integer
End Class
Public Class Candidates
Public Property result As Result
Public Property timestamp As Double
Public Property source As String
Public Property event_type As String
Public Property authorization As Authorization
Public Property template As Template
Public Property candidate As Candidate
End Class
Public Function GetData(ApiEndpoint As Uri, ApiToken As String)
Dim origResponse As HttpWebResponse = Nothing
Dim objResponse As HttpWebResponse = Nothing
Dim origReader As StreamReader = Nothing
Dim objReader As StreamReader = Nothing
Dim origRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(ApiEndpoint), HttpWebRequest)
origRequest.Headers.Add("Authorization", "Basic " & ApiToken)
origRequest.AllowAutoRedirect = False
origRequest.Method = "GET"
'Call the API and get the JSON Response data
origResponse = DirectCast(origRequest.GetResponse(), HttpWebResponse)
Dim Stream As Stream = origResponse.GetResponseStream()
Dim sr As New StreamReader(Stream, Encoding.GetEncoding("utf-8"))
Dim myJsonResponse As String = sr.ReadToEnd()
'Deserialize the Json
Dim objFormResults As Result = JsonConvert.DeserializeObject(Of Result)(myJsonResponse)
'loop through the results
End Function
I need to obtain the "title" from the response i get from Youtube.
I get the following error at line 8, position 12, that is, at line:
"items": [
just after the "["
The error i get is:
Exception: Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll ("Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'categoryid.Item' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> 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 'items', line 8, position 12."). Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll ("Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'categoryid.Item' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> 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 'items', line 8, position 12.") 2.61s [12096] <No Name>
I have this code:
Dim m As IEnumerable(Of Rootobject) = JsonConvert.DeserializeObject(Of IEnumerable(Of Rootobject))(res)
with this JSON data:
{
"kind":"youtube#videoListResponse",
"etag":"\"m2yskBQFythfE4irbTIeOgYYfBU/jznkhy3_Aws9VtommTkcdOYnAAk\"",
"pageInfo":{
"totalResults":1,
"resultsPerPage":1
},
"items":[
{
"kind":"youtube#video",
"etag":"\"m2yskBQFythfE4irbTIeOgYYfBU/M_wQmC4lQBaHJGxo79N7WlmqNr8\"",
"id":"SSbBvKaM6sk",
"snippet":{
"publishedAt":"2009-04-15T20:31:11.000Z",
"channelId":"UC2kTZB_yeYgdAg4wP2tEryA",
"title":"Blur - Song 2",
"description":"Blur 21 -- Celebrating 21 years of Blur. To find out more, click here:http://smarturl.it/blur21y\n\n#blur21\n \nFollow Blur on Twitter:www.twitter.com/blurofficial \nFind Blur on Facebook:www.facebook.com/blur\n\nMusic video by Blur performing Song 2.",
"thumbnails":{
"default":{
"url":"https://i.ytimg.com/vi/SSbBvKaM6sk/default.jpg",
"width":120,
"height":90
},
"medium":{
"url":"https://i.ytimg.com/vi/SSbBvKaM6sk/mqdefault.jpg",
"width":320,
"height":180
},
"high":{
"url":"https://i.ytimg.com/vi/SSbBvKaM6sk/hqdefault.jpg",
"width":480,
"height":360
},
"standard":{
"url":"https://i.ytimg.com/vi/SSbBvKaM6sk/sddefault.jpg",
"width":640,
"height":480
},
"maxres":{
"url":"https://i.ytimg.com/vi/SSbBvKaM6sk/maxresdefault.jpg",
"width":1280,
"height":720
}
},
"channelTitle":"emimusic",
"tags":[
"Blur",
"Song"
],
"categoryId":"10",
"liveBroadcastContent":"none",
"localized":{
"title":"Blur - Song 2",
"description":"Blur 21 -- Celebrating 21 years of Blur. To find out more, click here:http://smarturl.it/blur21y\n\n#blur21\n \nFollow Blur on Twitter:www.twitter.com/blurofficial \nFind Blur on Facebook:www.facebook.com/blur\n\nMusic video by Blur performing Song 2."
}
},
"contentDetails":{
"duration":"PT2M3S",
"dimension":"2d",
"definition":"sd",
"caption":"false",
"licensedContent":true,
"regionRestriction":{
"allowed":[
"BY",
"US"
]
},
"projection":"rectangular"
}
}
]
}
and these classes. the classes are pasted as special with the visual studio 2015 option.
Public Class Rootobject
Public Property kind As String
Public Property etag As String
Public Property pageInfo As Pageinfo
Public Property items() As Item
End Class
Public Class Pageinfo
Public Property totalResults As Integer
Public Property resultsPerPage As Integer
End Class
Public Class Item
Public Property kind As String
Public Property etag As String
Public Property id As String
Public Property snippet As Snippet
Public Property contentDetails As Contentdetails
End Class
Public Class Snippet
Public Property publishedAt As Date
Public Property channelId As String
Public Property title As String
Public Property description As String
Public Property thumbnails As Thumbnails
Public Property channelTitle As String
Public Property tags() As String
Public Property categoryId As String
Public Property liveBroadcastContent As String
Public Property localized As Localized
End Class
Public Class Thumbnails
Public Property _default As _Default
Public Property medium As Medium
Public Property high As High
Public Property standard As Standard
Public Property maxres As Maxres
End Class
Public Class _Default
Public Property url As String
Public Property width As Integer
Public Property height As Integer
End Class
Public Class Medium
Public Property url As String
Public Property width As Integer
Public Property height As Integer
End Class
Public Class High
Public Property url As String
Public Property width As Integer
Public Property height As Integer
End Class
Public Class Standard
Public Property url As String
Public Property width As Integer
Public Property height As Integer
End Class
Public Class Maxres
Public Property url As String
Public Property width As Integer
Public Property height As Integer
End Class
Public Class Localized
Public Property title As String
Public Property description As String
End Class
Public Class Contentdetails
Public Property duration As String
Public Property dimension As String
Public Property definition As String
Public Property caption As String
Public Property licensedContent As Boolean
Public Property regionRestriction As Regionrestriction
Public Property projection As String
End Class
Public Class Regionrestriction
Public Property allowed() As String
End Class
What i need to do?
Thanks in advance.
You are getting that exception because you have declared several auto-implemented array properties incorrectly. As shown in Auto-Implemented Properties (Visual Basic) such properties should be declared as follows, with the () array indicator as part of the return type:
Public Property items As Item()
Instead you declare them as follows:
Public Property items() As Item
This declares a property returning a single Item rather than an array of them. The () attached to the property name is optional; for parameterless properties it is redundant but for properties taking parameters the argument list appears there. See the documentation page Property Statement for details. Then Json.NET throws the exception you see when attempting to deserialize a JSON array into one of the non-array properties in your model.
To fix this, three of your classes should be modified as follows:
Public Class Rootobject
Public Property kind As String
Public Property etag As String
Public Property pageInfo As Pageinfo
Public Property items As Item() ' Fixed
End Class
Public Class Snippet
Public Property publishedAt As Date
Public Property channelId As String
Public Property title As String
Public Property description As String
Public Property thumbnails As Thumbnails
Public Property channelTitle As String
Public Property tags As String() ' Fixed
Public Property categoryId As String
Public Property liveBroadcastContent As String
Public Property localized As Localized
End Class
Public Class Regionrestriction
Public Property allowed As String() ' Fixed
End Class
Then, since the items are an array, to access the titles and put then in a list, you can use extension methods from System.Linq.Enumerable:
' Convert the json string to RootObject.
Dim root = JsonConvert.DeserializeObject(Of Rootobject)(json)
' Extract the list of titles.
Dim titles = root.items _
.Select(Function(i) i.snippet.title) _
.ToList()
' Get the first title in the list.
Dim firstTitle = titles.FirstOrDefault()
Console.WriteLine("First title = ""{0}""", firstTitle)
Which prints out First title = "Blur - Song 2".
Sample VB.Net fiddle.
I have this json response:
{
"tracked_until": "1483704963",
"solo_competitive_rank": "4066",
"competitive_rank": "3821",
"mmr_estimate": {
"estimate": 3971,
"stdDev": 215.26495302301302,
"n": 20
},
"profile": {
"account_id": 131505839,
"personaname": "LeG",
"name": null,
"cheese": 1,
"steamid": "76561198091771567",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c.jpg",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c_medium.jpg",
"avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c_full.jpg",
"profileurl": "http://steamcommunity.com/id/LegLabs/",
"last_login": "2016-11-11T13:13:18.651Z",
"loccountrycode": "AL"
}
}
Using an Online Tool, I created these classes:
<Serializable>
Public Class mmr_estimate
Public Property estimate As String
Public Property stdDev As String
Public Property n As String
End Class
<Serializable>
Public Class profile
Public Property account_id As String
Public Property personaname As String
Public Property name As String
Public Property cheese As String
Public Property steamid As String
Public Property avatar As String
Public Property avatarmedium As String
Public Property avatarfull As String
Public Property profileurl As String
Public Property last_login As String
Public Property loccountrycode As String
End Class
<Serializable>
Public Class RootObject
Public Property tracked_until As String
Public Property solo_competitive_rank As String
Public Property competitive_rank As String
Public Property mmr_estimate As mmr_estimate
Public Property profile As profile
End Class
Then I use this code to deserialize it:
Dim steamData As String = ' the json contents above
Dim myjss As New JavaScriptSerializer()
Dim playerDictionary = myjss.Deserialize(Of List(Of RootObject))(steamData)
But the result I get is nothing, playerDictionary has 0 items, when it should have 1 item with the contents of the json parsed into KeyValuePairs.
If I use this piece of code
Dim data = myjss.DeserializeObject(steamData)
and then run a for each loop on the data elements, I can see the contents of data when debugging, but I don't know how to work with them like that, since they are just objects which I'm having trouble converting into KeyValuePairs, who in themselves may contain arrays of KeyValuePairs.
What I'm trying to get is the values of solo_competitive_rank, competitive_rank and steamid, but if I can't get the whole contents deserialized, I can't do that.
Are the declared classes wrong?
but with this solution you use not the class RootObject.
With first method, your JSON want in the list with key and value.
This is only for JSON Array. :(
rg