Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple working Example of json.net in VB.net

Tags:

I have the following simplified JSON string from a provider, its been a long time since I used Visual Studio and vb.Net, so I'm very rusty!

{ "Venue": {     "ID": 3145,     "Name": "Big Venue, Clapton",     "NameWithTown": "Big Venue, Clapton, London",     "NameWithDestination": "Big Venue, Clapton, London",     "ListingType": "A",     "Address": {         "Address1": "Clapton Raod",         "Address2": "",         "Town": "Clapton",         "County": "Greater London",         "Postcode": "PO1 1ST",         "Country": "United Kingdom",         "Region": "Europe"     },     "ResponseStatus": {         "ErrorCode": "200",         "Message": "OK"     } } } 

I want to use JSON.Net to turn this in to something I can work with, I have read examples etc and JSON.net looks like the answer, but I'm getting no where.

My .Net code (Me.TextBox1.Text contains the JSON shown above)

Imports Newtonsoft.Json  Public Class Form1  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click     Dim obj As JSON_result     obj = JsonConvert.DeserializeObject(Of JSON_result)(Me.TextBox1.Text)      MsgBox(obj.ID) End Sub End Class  Public Class JSON_result     Public ID As Integer     Public Name As String     Public NameWithTown As String     Public NameWithDestination As String     Public ListingType As String  End Class 

Can someone explain why obj.ID always ends up as 0 please, and why none of the other properties of my class are populated and what I need to do to fix this, no errors are reported.

like image 944
Great Big Al Avatar asked Feb 10 '14 12:02

Great Big Al


People also ask

What is JSON in VB net?

JSON , Known As JavaScript Object Notation , Is An Important Format Which We Mostly Get As Output When Requesting Data By API. In This Post We Will See How To Read , Parse JSON Using Visual Basic .NET (VB.NET)

What is JSON explain with example?

JSON is a text-based data format that is used to store and transfer data. For example, // JSON syntax { "name": "John", "age": 22, "gender": "male", } In JSON, the data are in key/value pairs separated by a comma , . JSON was derived from JavaScript. So, the JSON syntax resembles JavaScript object literal syntax.

How does JSON work?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).


2 Answers

Your class JSON_result does not match your JSON string. Note how the object JSON_result is going to represent is wrapped in another property named "Venue".

So either create a class for that, e.g.:

Public Class Container     Public Venue As JSON_result End Class  Public Class JSON_result     Public ID As Integer     Public Name As String     Public NameWithTown As String     Public NameWithDestination As String     Public ListingType As String End Class  Dim obj = JsonConvert.DeserializeObject(Of Container)(...your_json...) 

or change your JSON string to

{     "ID": 3145,     "Name": "Big Venue, Clapton",     "NameWithTown": "Big Venue, Clapton, London",     "NameWithDestination": "Big Venue, Clapton, London",     "ListingType": "A",     "Address": {         "Address1": "Clapton Raod",         "Address2": "",         "Town": "Clapton",         "County": "Greater London",         "Postcode": "PO1 1ST",         "Country": "United Kingdom",         "Region": "Europe"     },     "ResponseStatus": {         "ErrorCode": "200",         "Message": "OK"     } } 

or use e.g. a ContractResolver to parse the JSON string.

like image 68
sloth Avatar answered Jan 03 '23 11:01

sloth


Imports Newtonsoft.Json.Linq  Dim json As JObject = JObject.Parse(Me.TextBox1.Text) MsgBox(json.SelectToken("Venue").SelectToken("ID")) 
like image 28
Dibu Avatar answered Jan 03 '23 12:01

Dibu