JSON class creation help in VB.NET [duplicate] - json

I am very new to json, JSON.net and all that. After reading similiar questions here I cannot get my code working.
What exactly is my error? What have I overseen?
Is it possible to skip the classes "links" and "meta" for testing purposes or do I have to define EVERY property?
I have got the following REST output:
{
"codes" : [
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/4Sxnr961xzM",
"rel" : "document_field_definition_code",
"title" : "TITLE 1"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/buho0CsLc5k",
"rel" : "document_field_definition_code",
"title" : "TITLE 2"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/RvQoykUM_Sk",
"rel" : "document_field_definition_code",
"title" : "TITLE 3"
}
],
"links" : [
{
"about" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes?about=1",
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes",
"method" : "GET",
"rel" : "self",
"title" : null,
"type" : "codes"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes",
"method" : "POST",
"rel" : "codes",
"title" : "create new codes entity"
}
],
"meta" : {
"description" : null,
"last_page" : 1,
"page_offset" : 0,
"page_size" : 50,
"query-template" : "/codes{?query_search,page_offset,page_size,query_identification,embedded,properties,about}",
"total" : 6
}
}
As I unterstood I need three classes: e.g. codes, links and meta.
I created a class "clscodes":
Public Class clsCode
Private m_href As String
Private m_rel As String
Private m_title As String
Public Property Href As String
Get
Return m_href
End Get
Set(value As String)
m_href = value
End Set
End Property
Public Property Rel As String
Get
Return m_rel
End Get
Set(value As String)
m_rel = value
End Set
End Property
Public Property Title As String
Get
Return m_title
End Get
Set(value As String)
m_title = value
End Set
End Property
End Class
And I created a class clsValuelist:
Public Class clsWerteliste
Private m_code As IList(Of clsCode)
Public Property Code() As clsCode()
Get
Return m_code
End Get
Set(value As clsCode())
m_code = value
End Set
End Property
End Class
When I try to deserialize it I get "nothing" as in "CoolOutput"
Dim CoolOutput As New clsWerteliste
CoolOutput = JsonConvert.DeserializeObject(Of clsWerteliste)(jsonstring)

Your classes are pretty close, it looks like you possibly tried to pretty things up a bit such as changing codes to Codes but in so doing the properties no longer match. You can change class names but not property names (at least not that way):
Public Class CodeLinkContainer
<JsonProperty("codes")>
Public Property Codes As IList(Of Code)
<JsonProperty("links")>
Public Property Links As IList(Of Link)
<JsonProperty("meta")>
Public Property Meta As Meta
End Class
Public Class Meta
Public Property description As Object
Public Property last_page As Integer
Public Property page_offset As Integer
Public Property page_size As Integer
Public Property querytemplate As String
Public Property total As Integer
End Class
Public Class Code
Public Property href As String
Public Property rel As String
Public Property title As String
End Class
Public Class Link
Public Property about As String
Public Property href As String
Public Property method As String
Public Property rel As String
Public Property title As String
Public Property type As String
End Class
Using AutoImplement properties, available for some time now, means you can skip all the Get, Set boilerplate code. VS will create the classes for you also:
Edit Menu -> Paste Special -> Paste Json As Classes
You sometimes have to tweak the class if there is an array/list property. For instance, the robots may write:
Public Property elements() As Element
When it should be:
Public Property elements As Element()
The container class shows how to use <JsonProperty("pname")> to change the property name if you wish. This often needs to be done to create an alias for a property name which is a key word in VB (Return, Error etc). In this case, I changed codes and links to be Lists as you did.
Dim jstr = ... from whereever
Dim CodeLinks = JsonConvert.DeserializeObject(Of CodeLinkContainer)(jstr)
Console.WriteLine(CodeLinks.meta.total)
For Each Item In CodeLinks.codes
Console.WriteLine(Item.title)
Next
Result:
6
TITLE 1
TITLE 2
TITLE 3

Related

Deserialize nested JSON and VB.Net

I try to get the values from the basic section in the following json result
{
"responseCode": "Ok",
"responseMessage": "",
"ssnStatus": "Active",
"basic": {
"firstName": "Testnamn",
"givenName": "Gettnamn",
"surName": "Testname",
"middleName": null,
"lastName": "Lastname",
"co": null,
"street": "Teststreet lgh 1202",
"zipCode": "92609",
"city": "Stad"
},
"phoneNumbers": {
"phoneNumbers": []
},
"ssnStatusBlock": null
}
I can get the first level (ssnStatus) with the code below, but how do I get the firstName, givenName etc?
Dim post As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
Dim ssnStatus As String = post.ssnStatus
and
Public Class Post
Public Property ssnStatus As String
End Class
You are missing the properties and classes definition for all the other membersof the JSON object.
Create a new class file in your Project. Give it a name that properly describe the JSON usage,
add the Imports Newtonsoft.Json import,
copy a sample of the JSON object that describes the JSON structure (the one you have here is good),
position the caret inside the new class definition,
see the Visual Studio menu: Edit -> Paste Special -> Paste JSON as classes
Visual Studio will create all the classes and properties needed to handle the JSON object you selected.
Note: With more complex classes, it may happen that the result Visual Studio produces is not exactly what you require. In this case, try one of the specialized WebSites that provide a free conversion service:
Json Utils (VB.Net, C#, Java, Javascript, more...)
QuickType (C#, C++, Java, Javascript, Python, Go, more...)
json2csharp (C#)
JSON Formatter (JSON formatting and validation)
The new root class definition will be named Rootobject. Change this name as needed,
to make it more clear what the class is used for.
This is the class definition that Visual Studio creates with the JSON object in your question.
I created a class Project class named MyWebSitePost, created the JSON bject class definition as previously described, I then renamed the default master class Post, replacing the default Rootobject name:
Public Class MyWebSitePost
Public Class Post
Public Property responseCode As String
Public Property responseMessage As String
Public Property ssnStatus As String
Public Property basic As Basic
Public Property phoneNumbers As Phonenumbers
Public Property ssnStatusBlock As Object
End Class
Public Class Basic
Public Property firstName As String
Public Property givenName As String
Public Property surName As String
Public Property middleName As Object
Public Property lastName As String
Public Property co As Object
Public Property street As String
Public Property zipCode As String
Public Property city As String
End Class
Public Class Phonenumbers
Public Property phoneNumbers() As Object
End Class
End Class
You can then use the code you already have to access all the other properties:
(Some Properties type may have been set to Object; modify as
required).
Dim JsonPost As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
Dim ssnStatus As String = JsonPost.ssnStatus
Dim FirstName As String = JsonPost.basic.firstName
and so on.
Note:
As you can see, all properties have the default name, as defined in the JSON object. Some properties names are not really adeguate to describe thier content. For example the co property is probably the Country name. To change the property name, you can use the <JsonProperty> attribute, which references the JSON object original name and use a custom name for the Property:
'(...)
<JsonProperty("co")>
Public Property Country As Object
'(...)
You can then access this Property using the custom name:
Dim CountryName As String = JsonPost.basic.Country

Deserialize JSON with vb.net

This is my manually created class
Public Class ZohoList
Public Property Select_Store() As String
Get
Return m_Select_Store
End Get
Set
m_Select_Store = Value
End Set
End Property
Private m_Select_Store As String
End Class
Public Class RootObject
Public Property Zoho_List As List(Of ZohoList)
Get
Return m_Zoho_List
End Get
Set
m_Zoho_List = Value
End Set
End Property
Private m_Zoho_List As List(Of ZohoList)
End Class
After i get JSON response like this
{
"Store_Money_Snapshot":[
{
"TODO":"YES",
"Date_field":"10-May-2018",
"Xpawn_Money":"3562",
"Select_Store":"TEST",
"Total_Counted_Money":"$ 3,000.00",
"Store_from_Xpawn_pc2":"TEST",
"Discrepancy_Amount":"$ -562.00",
"Store_Problem_fixed":"NO",
"ID":"1111111111111111111",
"Image":"",
"Store_Closing_Balance":"$ 33,482.00"
},
{
"TODO":"YES",
"Date_field":"10-May-2018",
"Xpawn_Money":"10234",
"Select_Store":"TEST2",
"Total_Counted_Money":"$ 9,800.00",
"Store_from_Xpawn_pc2":"TEST2",
"Discrepancy_Amount":"$ -434.00",
"Store_Problem_fixed":"NO",
"ID":"2222222222222",
"Image":"",
"Store_Closing_Balance":"$ 33,482.00"
}
]
}
My vb.net code for deserializing object is put in two lines
Dim myO = JsonConvert.DeserializeObject(Of RootObject)(response)
Dim items = myO.Zoho_List
For Each item In items
lTodo.Add(item.Select_Store.ToString)
'Now comes th code
Next
From entire response i only need the Select_Store value so in class i put only that value
Also i tried put all values in my class but still it wont deserialize JSON response
Your RootObject is paired with the first curly brace {.
There is then one property on that "root object' in the json: Store_Money_Snapshot, which doesn't appear anywhere in your RootObject.
Store_Money_Snapshot is an array or List<> or objects. These objects contain your Select_Store property.
So something like this should get you moving:
Public Class RootObject
' RootObject is a HORRIBLE name.
Public Property Store_Money_Snapshot As List(Of ZohoList)
End Class
Public Class ZohoList
' Again, ZohoList is a HORRIBLE name.
Public Property Select_Store As String
End Class
I strongly encourage you to give some thought to naming your classes with more accurate descriptive names.

Organizing a JSON into some text boxes in VB.net [duplicate]

I am very new to json, JSON.net and all that. After reading similiar questions here I cannot get my code working.
What exactly is my error? What have I overseen?
Is it possible to skip the classes "links" and "meta" for testing purposes or do I have to define EVERY property?
I have got the following REST output:
{
"codes" : [
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/4Sxnr961xzM",
"rel" : "document_field_definition_code",
"title" : "TITLE 1"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/buho0CsLc5k",
"rel" : "document_field_definition_code",
"title" : "TITLE 2"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes/RvQoykUM_Sk",
"rel" : "document_field_definition_code",
"title" : "TITLE 3"
}
],
"links" : [
{
"about" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes?about=1",
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes",
"method" : "GET",
"rel" : "self",
"title" : null,
"type" : "codes"
},
{
"href" : "https://www.someserver.com/form_definitions/OIlG4GxMyeh0sdrt3AYuaXbauGicW71M/field_definitions/zFEova6LiPM/codes",
"method" : "POST",
"rel" : "codes",
"title" : "create new codes entity"
}
],
"meta" : {
"description" : null,
"last_page" : 1,
"page_offset" : 0,
"page_size" : 50,
"query-template" : "/codes{?query_search,page_offset,page_size,query_identification,embedded,properties,about}",
"total" : 6
}
}
As I unterstood I need three classes: e.g. codes, links and meta.
I created a class "clscodes":
Public Class clsCode
Private m_href As String
Private m_rel As String
Private m_title As String
Public Property Href As String
Get
Return m_href
End Get
Set(value As String)
m_href = value
End Set
End Property
Public Property Rel As String
Get
Return m_rel
End Get
Set(value As String)
m_rel = value
End Set
End Property
Public Property Title As String
Get
Return m_title
End Get
Set(value As String)
m_title = value
End Set
End Property
End Class
And I created a class clsValuelist:
Public Class clsWerteliste
Private m_code As IList(Of clsCode)
Public Property Code() As clsCode()
Get
Return m_code
End Get
Set(value As clsCode())
m_code = value
End Set
End Property
End Class
When I try to deserialize it I get "nothing" as in "CoolOutput"
Dim CoolOutput As New clsWerteliste
CoolOutput = JsonConvert.DeserializeObject(Of clsWerteliste)(jsonstring)
Your classes are pretty close, it looks like you possibly tried to pretty things up a bit such as changing codes to Codes but in so doing the properties no longer match. You can change class names but not property names (at least not that way):
Public Class CodeLinkContainer
<JsonProperty("codes")>
Public Property Codes As IList(Of Code)
<JsonProperty("links")>
Public Property Links As IList(Of Link)
<JsonProperty("meta")>
Public Property Meta As Meta
End Class
Public Class Meta
Public Property description As Object
Public Property last_page As Integer
Public Property page_offset As Integer
Public Property page_size As Integer
Public Property querytemplate As String
Public Property total As Integer
End Class
Public Class Code
Public Property href As String
Public Property rel As String
Public Property title As String
End Class
Public Class Link
Public Property about As String
Public Property href As String
Public Property method As String
Public Property rel As String
Public Property title As String
Public Property type As String
End Class
Using AutoImplement properties, available for some time now, means you can skip all the Get, Set boilerplate code. VS will create the classes for you also:
Edit Menu -> Paste Special -> Paste Json As Classes
You sometimes have to tweak the class if there is an array/list property. For instance, the robots may write:
Public Property elements() As Element
When it should be:
Public Property elements As Element()
The container class shows how to use <JsonProperty("pname")> to change the property name if you wish. This often needs to be done to create an alias for a property name which is a key word in VB (Return, Error etc). In this case, I changed codes and links to be Lists as you did.
Dim jstr = ... from whereever
Dim CodeLinks = JsonConvert.DeserializeObject(Of CodeLinkContainer)(jstr)
Console.WriteLine(CodeLinks.meta.total)
For Each Item In CodeLinks.codes
Console.WriteLine(Item.title)
Next
Result:
6
TITLE 1
TITLE 2
TITLE 3

vb.net Serializing Collections unable to add a collection to object?

I'm trying to generate some JSON that looks like this:
{
"#type": "MessageCard",
"sections": [
{
"activityTitle": " Request",
"facts": [
{
"name": "name1",
"value": "Value"
},
{
"name": " Date:",
"value": "Value Date"
}
],
"text": "Some Test."
}
],
"potentialAction": [
{
"#type": "ActionCard",
"name": "Add a comment",
"inputs": [
{
"#type": "TextInput",
"id": "comment",
"isMultiline": true
}
]
}
]
}
I performed a paste special into VS and it generated the class structure for me as such:
Public Class MessageCard
Public Property type As String
Public Property context As String
Public Property summary As String
Public Property themeColor As String
Public Property sections() As Section
Public Property potentialAction() As Potentialaction
End Class
I'm trying to add the sections to the object as such:
Dim m as New MessageCard
Dim s As New List(Of Section)
s.Add(s1)
s.Add(s2)
m.sections = s
The compiler complains that it cannot convert a list of Sections into a Section. Did the class get generated incorrectly, or am i constructing it incorrectly?
First, your JSON is not quite complete and the Classes you show wont create that JSON.
As posted, that JSON simply shows a Sections and potentialAction class which are not related in any way. An enclosing [ ... ] is needed to represent the MessageCard class containing the two of them.
[{
"#type": "MessageCard",
...
}]
Next, the class you have shows all sorts of things not present in the JSON: context, summary and themeColor for instance. I assume those might be missing for brevity, but it is confusing. There is also 2 other Types missing which are in the JSON, Fact and Input.
Corrected, the classes should be:
Public Class MsgCard
<JsonProperty("#type")>
Public Property ItemType As String
Public Property sections As List(Of Section)
Public Property potentialAction As List(Of Potentialaction)
Public Sub New()
sections = New List(Of Section)
potentialAction = New List(Of Potentialaction)
End Sub
End Class
Public Class Section
Public Property activityTitle As String
Public Property facts As Fact()
Public Property text As String
End Class
Public Class Fact
Public Property name As String
Public Property value As String
End Class
Public Class Potentialaction
<JsonProperty("#type")>
Public Property ActionType As String
Public Property name As String
Public Property inputs As Input()
End Class
Public Class Input
<JsonProperty("#type")>
Public Property InputType As String
Public Property id As String
Public Property isMultiline As Boolean
End Class
Notes
You did not specify a JSON serializer, so this is prepared to JSON.NET as recommended by Microsoft.
#type is an illegal property name, so the JsonProperty attribute is used to create an alias. I also used less confusingly redundant names.
You may want to change Fact and Input to List(Of T) if you will be creating and pushing them into the class object as well.
Finally, for the actual question you asked, most of the automatic class generators have trouble with arrays (even VS).
Public Property sections() As Section
' should be:
Public Property sections As Section()
That simply declares that sections will be an array, it does not create the array. Normally this is not a problem because the Serializer/Deserializer will create the array. To allow code external to the class to add to them, you probably want to use a List as the classes above do, then create the instance in the constructor:
Public Sub New()
sections = New List(Of Section)
potentialAction = New List(Of Potentialaction)
End Sub

JSON Format Deserialize Error

I am new to manipulating JSON format data retrieved through an Web API. I'm using Newtonsoft.Json.JsonConvert to deserialize and assign to datatable.
This is the JSON data I am trying to get into a datatable.
[{
"classDesc":"SIDEWALK,DRIVEWAY,CURB",
"classCode":"EH",
"legend":"017",
"isActive":"Y",
"atrSpaCityDistrictId":"00D17209F8F25F6D4A00011302",
"atrSpaCitieDistrict":{
"cityDistrict":"",
"isActive":"1",
"atrSpaClassLegends":null,
"id":"00D17209F8F25F6D4A00011302"
},
"id":"00D1748B8DA0AB0A7400011202"
}]
I have created the below classes
Public Class RootObject
Public Property classDesc As String
Public Property classCode As String
Public Property id As Integer
Public Property legend As Integer
Public Property isActive As String
Public Property atrSpaCityDistrictId As Integer
Public Property atrSpaCitieDistrict As List(Of Result) End Class
Public Class Result
Public Property cityDistrict As String
Public Property isActive As Integer
Public Property atrSpaClassLegends As List(Of Legend)
Public Property id As Integer
End Class
Public Class Legend
End Class
Below is my VB.Net code:
Dim table As DataTable Dim client As New HttpClient() client.BaseAddress = New Uri("http://localhost:5000/") client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim response As HttpResponseMessage = client.GetAsync("api/spaclasslegend").Result
If response.IsSuccessStatusCode Then
Dim json As String = response.Content.ReadAsStringAsync().Result table = JsonConvert.DeserializeObject(Of DataTable)(json)
End If
Getting below error
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'Alco.APTIS.Services.RootObject' because the type requires a JSON
object (e.g. {"name":"value"}) to deserialize correctly."
I have tried different ways to fix this using stackoverflow.com and finally posting here for help.
You have two unrelated issues.
Firstly, your root object doesn't match your JSON. If I copy your JSON into a code generator such as http://jsonutils.com/, I get the following, using a List(Of Legend) for the null atrSpaClassLegends property:
Public Class AtrSpaCitieDistrict
Public Property cityDistrict As String
Public Property isActive As String
Public Property atrSpaClassLegends As List(Of Legend)
Public Property id As String
End Class
Public Class RootObject
Public Property classDesc As String
Public Property classCode As String
Public Property legend As String
Public Property isActive As String
Public Property atrSpaCityDistrictId As String
Public Property atrSpaCitieDistrict As AtrSpaCitieDistrict
Public Property id As String
End Class
Public Class Legend
End Class
Having done this, it is now possible to deserialize your JSON into a List(Of RootObject):
Dim root = JsonConvert.DeserializeObject(Of List(Of RootObject))(jsonString)
Note you must deserialize as a List(Of RootObject) (or similar .Net collection such as an array) since your top level JSON container is a JSON array -- an ordered comma-separated sequence of values surrounded by [ and ]. This is explained in the Json.NET Serialization Guide. Sample fiddle.
Secondly, despite having created these types, in the code shown in your question you are not using them at all! Instead, you are deserializing to a DataTable. And unfortunately the atrSpaCitieDistrict property is a nested object:
{
"atrSpaCitieDistrict": {
"cityDistrict": "",
"isActive": "1",
"atrSpaClassLegends": null,
"id": "00D17209F8F25F6D4A00011302"
},
}
Json.NET does not support automatically deserializing column values that are complex objects into untyped data tables, and will throw an exception if one is encountered. Instead, you would need to deserialize into a typed data table. However, this may be a typo in your question; the exception error message indicates you are actually trying to deserialize into an object of type Alco.APTIS.Services.RootObject and not a DataTable.