VB.NET deserialize Newtonsoft JSON into object dynamically - json

ANSWER given describes using LINQ to JSON and the JObject to convert from JSON to a workable object on the fly. Below is the completed code that takes me from my JSON to a workable object, followed by the original question.
'Parse string of JSON data into a JObject that can be accessed via VB.NET
Dim resultSet As JObject = JObject.Parse(responseBody)
'Data can be accessed as seen below
Dim cpu As String = CType(resultSet("KeyName"), String)
=========================================================================
I have a web app that will be making several API calls to a service called inContact (http://www.incontact.com/)
Each of the API calls will be receiving an HTTP response filled with JSON formatted in this way:
{
"resultSet": {
"_links": {
"self": "string",
"next": "string",
"previous": "string"
},
"businessUnitId": 0,
"lastPollTime": "2016-11-08T21:45:46.510Z",
"totalRecords": 0,
"agents": [
{
"agentId": 0,
"userName": "string",
"firstName": "string",
"middleName": "string",
"lastName": "string",
"emailAddress": "string",
"isActive": true,
"teamId": 0,
"teamName": "string",
"reportToId": 0,
"reportToName": "string",
"isSupervisor": true,
"lastLogin": "2016-11-08T21:45:46.510Z",
"lastUpdated": "2016-11-08T21:45:46.510Z",
"location": "string",
"custom1": "string",
"custom2": "string",
"custom3": "string",
"custom4": "string",
"custom5": "string",
"internalId": "string",
"profileId": 0,
"profileName": "string",
"timeZone": "string",
"country": "string",
"countryName": "string",
"state": "string",
"city": "string",
"chatRefusalTimeout": 0,
"phoneRefusalTimeout": 0,
"workItemRefusalTimeout": 0,
"defaultDialingPattern": 0,
"defaultDialingPatternName": "string",
"teamDefaultMaxChats": true,
"maxConcurrentChats": 0,
"notes": "string",
"createDate": "2016-11-08T21:45:46.510Z",
"inactiveDate": "2016-11-08T21:45:46.510Z",
"hireDate": "2016-11-08T21:45:46.510Z",
"terminationDate": "2016-11-08T21:45:46.510Z",
"rehireStatus": true,
"employmentType": 0,
"employmentTypeName": "Full-Time",
"referral": "string",
"atHome": true,
"hiringSource": "string",
"ntLoginName": "string",
"scheduleNotification": "5",
"federatedId": "string",
"sipUser": "string",
"useTeamMaxEmailInboxCount": true,
"maxEmailInboxCount": 0
}
]
}
}
I am deserializing the JSON with Newtonsoft. However, with each different call there will be different key/value pairs, such as this:
{
"resultSet": {
"businessUnitId": 0,
"lastPollTime": "2016-11-08T21:45:46.604Z",
"teams": [
{
"teamId": 0,
"teamName": "string",
"isActive": true,
"description": "string",
"notes": "string",
"lastUpdateTime": "2016-11-08T21:45:46.604Z",
"inViewEnabled": true,
"wfoEnabled": true,
"wfmEnabled": true,
"qmEnabled": true,
"maxConcurrentChats": 0,
"agentCount": 0,
"maxEmailInboxCount": true,
"inViewGamificationEnabled": true,
"inViewChatEnabled": true,
"inViewLMSEnabled": true,
"analyticsEnabled": true
}
],
"agents": [
{
"agentId": 0,
"firstName": "string",
"lastName": "string"
}
]
}
}
I currently am taking in the HTTP response and deserializing the JSON to create a workable .NET object. To do so this is my shortened code, omitting the HTTP request, assuming the request was successful:
If Not String.IsNullOrEmpty(responseBody) Then
' Success. Do something with the response.
'Declare object for holding the JSON from the API call for this call only (class does not fit other calls)
Dim resultSet As GetAgentsAPICall = New GetAgentsAPICall
'Deserialize JSON response into the new resultSet object
resultSet = JsonConvert.DeserializeObject(responseBody)
The issue is that I need a specific class for each API call, instead of just being able to take a string with JSON formatting and throw it into an object with properties matching the key/values. Below is the class I have that takes in just the above API call (the first one listed, longer in length):
Public Class GetAgentsAPICall
Public Property resultSet As Resultset
End Class
Public Class Resultset
Public Property _links As _Links
Public Property businessUnitId As Integer
Public Property lastPollTime As Date
Public Property totalRecords As Integer
Public Property agents() As Agent
End Class
Public Class _Links
Public Property self As String
Public Property _next As String
Public Property previous As String
End Class
Public Class Agent
Public Property agentId As Integer
Public Property userName As String
Public Property firstName As String
Public Property middleName As String
Public Property lastName As String
Public Property emailAddress As String
Public Property isActive As Boolean
Public Property teamId As Integer
Public Property teamName As String
Public Property reportToId As Integer
Public Property reportToName As String
Public Property isSupervisor As Boolean
Public Property lastLogin As Date
Public Property lastUpdated As Date
Public Property location As String
Public Property custom1 As String
Public Property custom2 As String
Public Property custom3 As String
Public Property custom4 As String
Public Property custom5 As String
Public Property internalId As String
Public Property profileId As Integer
Public Property profileName As String
Public Property timeZone As String
Public Property country As String
Public Property countryName As String
Public Property state As String
Public Property city As String
Public Property chatRefusalTimeout As Integer
Public Property phoneRefusalTimeout As Integer
Public Property workItemRefusalTimeout As Integer
Public Property defaultDialingPattern As Integer
Public Property defaultDialingPatternName As String
Public Property teamDefaultMaxChats As Boolean
Public Property maxConcurrentChats As Integer
Public Property notes As String
Public Property createDate As Date
Public Property inactiveDate As Date
Public Property hireDate As Date
Public Property terminationDate As Date
Public Property rehireStatus As Boolean
Public Property employmentType As Integer
Public Property employmentTypeName As String
Public Property referral As String
Public Property atHome As Boolean
Public Property hiringSource As String
Public Property ntLoginName As String
Public Property scheduleNotification As String
Public Property federatedId As String
Public Property sipUser As String
Public Property useTeamMaxEmailInboxCount As Boolean
Public Property maxEmailInboxCount As Integer
End Class
I'm trying to avoid having 20 or 30 similar lengthy classes prebuilt, and instead build a modifiable list or object on the fly. I will need to take the values and either store them in a database or modify them and use another API call to POST the values back. Does anyone have a best practice, or am I stuck with 20-30 huge class definitions?
Thanks for your time!

Parse it with a JObject, which can also be queried using LINQ (see Newtonsoft's LINQ to JSON API, which sits under the Newtonsoft.Json.Linq namespace):
JObject o = JObject.Parse(#"{
'CPU': 'Intel',
'Drives': [
'DVD read/writer',
'500 gigabyte hard drive'
]
}");
string cpu = (string)o["CPU"];
// Intel
string firstDrive = (string)o["Drives"][0];
// DVD read/writer
IList<string> allDrives = o["Drives"].Select(t => (string)t).ToList();
// DVD read/writer
// 500 gigabyte hard drive
The example is in C#, but you can find relevant VB.NET answers on StackOverflow (have a look at the answer here, for example).

Related

How do I use Deserialised JSON Data In VB.Net?

I am trying to access data which I think I have successfully deserialized from a JSON file in vb.net.
My program currently reads data into a string from a JSON file. I have been researching how to deserialize the JSON data into a class structure and used a visual studio feature to create the class structure for me.
My question is how do I access this data after it has been deserialized? I have looked through hundreds of pages of forums and stack overflow posts to no avail.
Here is the JSON data:
{
"total": 389097,
"offset": 0,
"items": [
{
"entity": "Play",
"arid": "mpdwyOB32n",
"played_time": "2020-02-21T06:21:26+0000",
"service_id": "triplej",
"recording": {
"entity": "Recording",
"arid": "mtNYq18DdY",
"title": "Supalonely",
"metadata": null,
"description": null,
"duration": 219,
"artists": [
{
"entity": "Artist",
"arid": "ma35vZnl5B",
"name": "Benee",
"artwork": [],
"links": [
{
"entity": "Link",
"arid": "mlb5b4r7A0",
"url": "http://musicbrainz.org/artist/c352c56b-a610-4fc4-884c-704547ef30f5",
"id_component": "c352c56b-a610-4fc4-884c-704547ef30f5",
"title": "MusicBrainz artist",
"mini_synopsis": null,
"short_synopsis": null,
"medium_synopsis": null,
"type": "service",
"provider": "musicbrainz",
"external": true
}
],
"is_australian": null,
"type": "primary",
"role": null
}
],
"releases": [
{
"entity": "Release",
"arid": "mr45LNQePX",
"title": "Stella & Steve",
"format": "EP",
"artwork": [],
"links": [
{
"entity": "Link",
"arid": "mlV0JZbO20",
"url": "http://musicbrainz.org/release-group/9d0271c7-6448-4cbf-a335-0221e34a73ae",
"id_component": "9d0271c7-6448-4cbf-a335-0221e34a73ae",
"title": "MusicBrainz release-group",
"mini_synopsis": null,
"short_synopsis": null,
"medium_synopsis": null,
"type": "service",
"provider": "musicbrainz",
"external": true
}
],
"artists": [
{
"entity": "Artist",
"arid": "ma35vZnl5B",
"name": "Benee",
"artwork": [],
"links": [
{
"entity": "Link",
"arid": "mlb5b4r7A0",
"url": "http://musicbrainz.org/artist/c352c56b-a610-4fc4-884c-704547ef30f5",
"id_component": "c352c56b-a610-4fc4-884c-704547ef30f5",
"title": "MusicBrainz artist",
"mini_synopsis": null,
"short_synopsis": null,
"medium_synopsis": null,
"type": "service",
"provider": "musicbrainz",
"external": true
}
],
"is_australian": null,
"type": "primary",
"role": null
}
],
"record_label": null,
"release_year": null,
"release_album_id": null
}
],
"artwork": [],
"links": [
{
"entity": "Link",
"arid": "mlwkZWz960",
"url": "http://musicbrainz.org/recording/2772cd61-892c-47ab-bc0e-1ca775ebe6f4",
"id_component": "2772cd61-892c-47ab-bc0e-1ca775ebe6f4",
"title": "MusicBrainz recording",
"mini_synopsis": null,
"short_synopsis": null,
"medium_synopsis": null,
"type": "service",
"provider": "musicbrainz",
"external": true
}
]
},
"release": {
"entity": "Release",
"arid": "mr45LNQePX",
"title": "Stella & Steve",
"format": "EP",
"artwork": [],
"links": [
{
"entity": "Link",
"arid": "mlV0JZbO20",
"url": "http://musicbrainz.org/release-group/9d0271c7-6448-4cbf-a335-0221e34a73ae",
"id_component": "9d0271c7-6448-4cbf-a335-0221e34a73ae",
"title": "MusicBrainz release-group",
"mini_synopsis": null,
"short_synopsis": null,
"medium_synopsis": null,
"type": "service",
"provider": "musicbrainz",
"external": true
}
],
"artists": [
{
"entity": "Artist",
"arid": "ma35vZnl5B",
"name": "Benee",
"artwork": [],
"links": [
{
"entity": "Link",
"arid": "mlb5b4r7A0",
"url": "http://musicbrainz.org/artist/c352c56b-a610-4fc4-884c-704547ef30f5",
"id_component": "c352c56b-a610-4fc4-884c-704547ef30f5",
"title": "MusicBrainz artist",
"mini_synopsis": null,
"short_synopsis": null,
"medium_synopsis": null,
"type": "service",
"provider": "musicbrainz",
"external": true
}
],
"is_australian": null,
"type": "primary",
"role": null
}
],
"record_label": null,
"release_year": null,
"release_album_id": null
}
}
]
}
Here is the class structure Visual Studio Generated:
Public Class JSON_Class
Public Class Rootobject
Public Property total As Integer
Public Property offset As Integer
Public Property items() As Item
End Class
Public Class Item
Public Property entity As String
Public Property arid As String
Public Property played_time As Date
Public Property service_id As String
Public Property recording As Recording
Public Property release As Release1
End Class
Public Class Recording
Public Property entity As String
Public Property arid As String
Public Property title As String
Public Property metadata As Object
Public Property description As Object
Public Property duration As Integer
Public Property artists() As Artist
Public Property releases() As Release
Public Property artwork() As Object
Public Property links() As Object
End Class
Public Class Artist
Public Property entity As String
Public Property arid As String
Public Property name As String
Public Property artwork() As Object
Public Property links() As Object
Public Property is_australian As Object
Public Property type As String
Public Property role As Object
End Class
Public Class Release
Public Property entity As String
Public Property arid As String
Public Property title As String
Public Property format As String
Public Property artwork() As Object
Public Property links() As Object
Public Property artists() As Artist1
Public Property record_label As Object
Public Property release_year As Object
Public Property release_album_id As Object
End Class
Public Class Artist1
Public Property entity As String
Public Property arid As String
Public Property name As String
Public Property artwork() As Object
Public Property links() As Object
Public Property is_australian As Object
Public Property type As String
Public Property role As Object
End Class
Public Class Release1
Public Property entity As String
Public Property arid As String
Public Property title As String
Public Property format As String
Public Property artwork() As Object
Public Property links() As Object
Public Property artists() As Artist2
Public Property record_label As Object
Public Property release_year As Object
Public Property release_album_id As Object
End Class
Public Class Artist2
Public Property entity As String
Public Property arid As String
Public Property name As String
Public Property artwork() As Object
Public Property links() As Object
Public Property is_australian As Object
Public Property type As String
Public Property role As Object
End Class
End Class
I deserialize my data using this line: (Where JSON_Class is the class structure and New_JSON_Content is the raw JSON)
Dim JSON_Wrapper = JsonConvert.DeserializeObject(Of JSON_Class)(New_JSON_Content)
What would be my next step if say I wanted to access any one specific variable from the populated class structure? Thanks!
The reason you can't see any data is that JSON_Class, the class you are deserializing to, contains no members. The code works; it's doing exactly what you're telling it to - create an instance of JSON_Class - which contains nothing - from this JSON string. JSON tokens which don't match anything in the deserialization type are ignored, which in this case is everything.
To make this work, deserialize to JSON_Class.Rootobject instead. You also don't need to nest your deserialization classes in a containing class.

Json Deserialize Nested Objects in Vb (Newtonsoft)

Basically, I'm trying to get data from the API.
My JSON is :
[
{
"id": 101,
"items": [
{
"id": 91,
"quantity": 141,
"size": "12",
"assigned": false,
"item": {
"pk": 27,
"title": "test title",
"description": "test description",
"designer": "designer",
"category": 4,
"size": "12,14,16",
"image": "media/products/indir.jpg",
"price": 31.0,
"resin_gr": 31.0
}
},
{
"id": 92,
"quantity": 18,
"size": "14",
"assigned": false,
"item": {
"pk": 26,
"title": "Bileklik 5",
"description": "bileklik",
"designer": "designer",
"category": 2,
"size": "12,14,16",
"image": "media/products/NB00316.png",
"price": 50.0,
"resin_gr": 14.0
}
}
]
}
]
My Classes are :
Public Class Orders
Public Property id As Integer
Public Property items As IEnumerable(Of OrderItems)
End Class
Public Class OrderItems
Public Property id As Integer
Public Property quantity As String
Public Property size As String
Public Property assigned As String
Public Property item As List(Of OrderProduct)
End Class
Public Class OrderProduct
Public Property pk As Integer
Public Property title As String
Public Property description As String
Public Property designer As String
Public Property category As String
Public Property size As String
Public Property image As String
Public Property price As String
Public Property resin_gr As String
End Class
I'm trying to parse JSON like this :
Dim result As IEnumerable(Of Orders) = JsonConvert.DeserializeObject(Of IEnumerable(Of Orders))(JSONDATA)
I'm getting error on Path '[0].items[0].item.pk', line 1, position 86.
What i tried :
i changed orders.item to list but nothing changed.
what shall i do about this ?
Public Class Orders
Public ID As Integer
Public Items As IEnumerable(Of OrderItems)
End Class
Public Class OrderItems
Public ID As Integer
Public quantity As Integer
Public size As Integer
Public assigned As Boolean
Public Item As OrderProduct
End Class
Public Class OrderProduct
Public pk As Integer
Public title As String
Public description As String
Public designer As String
Public category As Integer
Public size As String
Public image As String
Public price As Double
Public resin_gr As Double
End Class
Edit: like A Friend commented, This works because you were trying to parse an object as a list here:
Public Property item As List(Of OrderProduct)
When it needed to be
Public Item As OrderProduct
Somehow I missed that and solved the issue when I re-created the custom objects for the specified JSON.

Json not derserialising correctly

Json string is not deserialsing correctly
have played around with my classes etc, but bfevent always returns Nothing
have tried Public Property bfevent As BFEvent() in case it needs to be able to take multi values
pretty sure it's something simple....
strcatalogue = "{ "jsonrpc": "2.0", "result": [{ "event": { "id": "29202748", "name": "Kings XI Punjab v Mumbai Indians", "countryCode": "GB", "timezone": "GMT", "openDate": "2019-03-30T10:30:00.000Z" }, "marketCount": 20 }, { "event": { "id": "29201119", "name": "Victoria v NSW Blues", "countryCode": "AU", "timezone": "GMT", "openDate": "2019-03-27T23:30:00.000Z" }, "marketCount": 2 }, { "event": { "id": "29202753", "name": "Chennai Super Kings v Rajasthan Royals", "countryCode": "GB", "timezone": "GMT", "openDate": "2019-03-31T14:30:00.000Z" }, "marketCount": 35 }], "id": 1 }"
Dim objJson = JsonConvert.DeserializeObject(Of BFEventList)(strCatalogue)
Public Class BFEvent
Public Property id As String
Public Property name As String
Public Property countryCode As String
Public Property timezone As String
Public Property openDate As DateTime
End Class
Public Class BFResult
Public Property bfevent As BFEvent
Public Property marketCount As Integer
End Class
Public Class BFEventList
Public Property jsonrpc As String
Public Property result As BFResult()
Public Property id As Integer
End Class
BFEvent = Nothing
marketCount works fine, so something to do with BFEvents class
So i renamed my class to refer to the actual string values in the json. So bfevent is now event. I'm not sure if this is 'required' or not. Will test that elsewhere where i have working code.
But am now getting 'Keyword is not a valid identifier' for
Public Property Event As BFEvent()
with Event underlined.
No errors here
Dim Name As String = objJson.result(0).Event(0).name
But cannot compile due to above error

Iterate a deserealized .Net Object from Json to create a DataTable

I've being spending a lot of time thinking about how to iterate an object from a deserealized json which has many items, subitems and sub-subitems. I know there are a lot of questions already posted, but I still don't get it, 'cause the examples are differrents to mine and I'm a begineer developper. I get the Json from a Web Service and I pass the Token by headers and all that stuff, but I don't know the best way to manipulate this info. I'm using Newtonsoft library, BTW.
The Json is:
{
"orders": [
{
"customer": {
"id": "string",
"externalId": "string",
"firstName": "string",
"lastName": "string",
"name": "string",
"companyName": "string",
"emailAddresses": [
"string"
],
"phones": [
{
"type": "Mobile",
"number": "string"
}
],
"addresses": [
{
"lines": [
"string"
],
"locality": [
"string"
],
"region": "string",
"postcode": "string"
}
]
},
"vehicles": [
{
"id": "string",
"description": "string",
"vin": "string",
"odometer": 0,
"tag": "string",
"nextServiceDue": "2015-07-14T10:08:15Z",
"registrationTestDue": "2015-07-14T10:08:15Z",
"brand": "string",
"extendedWarrantyEndDate": 0,
"contractEndDate": 0,
"transmissionCode": "string",
"engineCode": "string",
"bodyType": "string",
"paintCode": "string",
"modelCode": "string",
"references": [
{
"key": "string",
"value": "string"
}
]
}
],
"employees": [
{
"id": "string",
"name": "string",
"type": "Technician"
}
],
"operations": [
{
"id": "35955",
"code": "RWB",
"externalCode": "RACS",
"description": "431151: Front Shock Absorber Assembly (One Side), Repair/Replace",
"isNew": true,
"labour": {
"pricing": {
"totals": {
"total": 0
}
},
"times": [
{
"quantity": 1.2
}
]
}
}
],
"parts": [
{
"number": "string",
"quantity": 1,
"quantityUnitOfMeasure": "Hour",
"description": "string",
"vehicleId": "string",
"pricing": {
"totals": {
"total": 0
}
}
}
]
}
],
"paging": {
"page": 0,
"morePages": true,
"resultCount": 0,
"pageCount": 0
}
}
And the Code for getting the json properties is:
Public Class Rootobject
Public Property orders() As Order
Public Property paging As Paging
End Class
Public Class Paging
Public Property page As Integer
Public Property morePages As Boolean
Public Property resultCount As Integer
Public Property pageCount As Integer
End Class
Public Class Order
Public Property customer As Customer
Public Property vehicles() As Vehicle
Public Property employees() As Employee
Public Property operations() As Operation
Public Property parts() As Part
End Class
Public Class Customer
Public Property id As String
Public Property externalId As String
Public Property firstName As String
Public Property lastName As String
Public Property name As String
Public Property companyName As String
Public Property emailAddresses() As String
Public Property phones() As Phone
Public Property addresses() As Address
End Class
Public Class Phone
Public Property type As String
Public Property number As String
End Class
Public Class Address
Public Property lines() As String
Public Property locality() As String
Public Property region As String
Public Property postcode As String
End Class
Public Class Vehicle
Public Property id As String
Public Property description As String
Public Property vin As String
Public Property odometer As Integer
Public Property tag As String
Public Property nextServiceDue As Date
Public Property registrationTestDue As Date
Public Property brand As String
Public Property extendedWarrantyEndDate As Integer
Public Property contractEndDate As Integer
Public Property transmissionCode As String
Public Property engineCode As String
Public Property bodyType As String
Public Property paintCode As String
Public Property modelCode As String
Public Property references() As Reference
End Class
Public Class Reference
Public Property key As String
Public Property value As String
End Class
Public Class Employee
Public Property id As String
Public Property name As String
Public Property type As String
End Class
Public Class Operation
Public Property id As String
Public Property code As String
Public Property externalCode As String
Public Property description As String
Public Property isNew As Boolean
Public Property labour As Labour
End Class
Public Class Labour
Public Property pricing As Pricing
Public Property times() As Time
End Class
Public Class Pricing
Public Property totals As Totals
End Class
Public Class Totals
Public Property total As Integer
End Class
Public Class Time
Public Property quantity As Single
End Class
Public Class Part
Public Property number As String
Public Property quantity As Integer
Public Property quantityUnitOfMeasure As String
Public Property description As String
Public Property vehicleId As String
Public Property pricing As Pricing1
End Class
Public Class Pricing1
Public Property totals As Totals1
End Class
Public Class Totals1
Public Property total As Integer
End Class
And I deserealize the Json and i've traying this, an example:
Dim JsonObject As List(Of Rootobject) = JsonConvert.DeserializeObject(Of List(Of Rootobject))(_JsonString)
For Each item In JsonObject
_PagPage = item.paging.page
_PagMorePag = item.paging.morePages
_PagResCount = item.paging.resultCount
_PagPagCount = item.paging.pageCount
Next
I tried to use Lists like List(Of Rootobject) or List(Of List(Of Dictionary(Of Rootoobject)), but it's still not working.
If you could to help me out i'd really appreciate it.

Deserialize JSON which can have different objects under same property name

I'm using JSON.NET to deserialize JSON responses from HTTP queries, but I'm stuck with an issue. That's because the response can send two types of object under same property, as shown below:
1st case sample (most common):
{
"type": "myType",
"tid": 4,
"action": "myAction",
"method": "myMethod",
"result": {
"success": true,
"total": 4,
"records": [
{
"id": 4,
"nome": "PRIMEIRO NOME",
"sigla": "PN"
},
{
"id": 1974,
"nome": "SEGUNDO NOME",
"sigla": "SN"
},
{
"id": 2584,
"nome": "TERCEIRO NOME",
"sigla": "TN"
},
{
"id": 1170,
"nome": "QUARTO NOME",
"sigla": "QN"
}
]
}
}
2nd case sample (rare):
{
"type": "myType",
"tid": 3,
"action": "myAction",
"method": "myMethod",
"result": [
{
"id": 4,
"nome": "PRIMEIRO NOME",
"sigla": "PN"
},
{
"id": 1974,
"nome": "SEGUNDO NOME",
"sigla": "SN"
},
{
"id": 2584,
"nome": "TERCEIRO NOME",
"sigla": "TN"
},
{
"id": 1170,
"nome": "QUARTO NOME",
"sigla": "QN"
}
]
}
I've been using these classes to receive data for the 1st case:
Public Class Record
Public Property id As Integer
Public Property nome As String
Public Property sigla As String
End Class
Public Class Result
Public Property success As Boolean
Public Property total As Integer
Public Property records As Record()
End Class
Public Class Response
Public Property type As String
Public Property tid As Integer
Public Property action As String
Public Property method As String
Public Property result As Result
End Class
So it worked well upon deserialization of 1st case JSON, with this statement:
Dim myResponse = JsonConvert.DeserializeObject(Of Response)(myJsonString)
However, when receiving 2nd case JSON, it creates object of type Response, stores in its property result an object of type Result whose properties remain empty, and the data which I needed to retrieve vanishes for it is stored nowhere.
I thought that I should modify the class Response so it has place for that different set of data, this way:
Public Class Response
Public Property type As String
Public Property tid As Integer
Public Property action As String
Public Property method As String
Public Property result As Result
Public Property resultRecords As Record()
End Class
The question, then, is this: how can I tell JsonConvert whether to store data in property Response.result when it fits its type (1st sample case above), or in Response.resultRecords in 2nd case?
Thank you!
Since the format of the JSON can vary for the same property, you will need a custom JsonConverter to handle it. The converter can read the JSON for the affected property, determine its format and then populate your objects appropriately. Here is the code you would need:
Public Class ResultConverter
Inherits JsonConverter
Public Overrides Function CanConvert(objectType As Type) As Boolean
Return objectType = GetType(Result)
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)
Dim result As Result = New Result
If token.Type = JTokenType.Array Then
result.records = token.ToObject(Of Record())(serializer)
result.total = result.records.Length
result.success = True
Else
serializer.Populate(token.CreateReader(), result)
End If
Return result
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
To use the converter, add a <JsonConverter> attribute to the result property in your Response class:
Public Class Response
Public Property type As String
Public Property tid As Integer
Public Property action As String
Public Property method As String
<JsonConverter(GetType(ResultConverter))>
Public Property result As Result
End Class
Here is a working fiddle to show that this converter will allow both JSON formats to be deserialized into the same classes: https://dotnetfiddle.net/NFbQ2Q