Solved

VB.NET - Extract Pairs out of a string (JSON)

Posted on 2016-10-21
4
32 Views
Last Modified: 2016-10-24
I use VB.Net and want to extract The Keypair values and put them into a class with the same variable (names) and values.

When I do a API call the outcome is shown as below.
How can I extract the KeyPair values? Like "Username":  "<text>"?

{
"License":
{
"LicenseKey": "Known",
"LicenseId": "****",
"Username": "++",
"Password": "*",
"ProductCode": "*",
"ProductName": "",
"Quantity": 1,
"PurchaseType": "Renew",
"CreatedDate": "28-07-2015",
"ExpirationDate": "27-07-2017",
"CustomerName": "*",
"CustomerCompany": "",
"CustomerEmail": "a@b.com",
"DiscountCode": 0,
"Status": "Active",
"BundleProductCode": "0",
"BundleProductName": "Advanced",
"BundleQuantity": 0,
"ELAPassword": "",
"CountryId": 60
},
"Result": true,
"Messages": []
}
0
Comment
Question by:jjvalstar
4 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41853679
you can create your custom class and then deserialize to get the values you want.

you can look for samples at Json.NET to do this

for more info
http://www.newtonsoft.com/json
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41853752
Import Newtonsoft.Json and try

Dim list As List(Of String) = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of String))(strJSON)

For Each item As String In list
	Console.WriteLine(item)
Next

Open in new window

0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 41854005
In other words:
Module Module1
	Dim data = "{""License"":{""LicenseKey"": ""Known"", ""LicenseId"": ""****"", ""Username"": ""++"", ""Password"": ""*"", ""ProductCode"": ""*"", ""ProductName"": """", ""Quantity"": 1, ""PurchaseType"": ""Renew"", ""CreatedDate"": ""28-07-2015"", ""ExpirationDate"": ""27-07-2017"", ""CustomerName"": ""*"", ""CustomerCompany"": """", ""CustomerEmail"": ""a@b.com"", ""DiscountCode"": 0, ""Status"": ""Active"", ""BundleProductCode"": ""0"", ""BundleProductName"": ""Advanced"", ""BundleQuantity"": 0, ""ELAPassword"": """", ""CountryId"": 60 }, ""Result"": true, ""Messages"": [] }"

	Sub Main()
		Dim [response] = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Response)(data)
		Console.WriteLine("UserName: {0}", [response].License.Username)
		Console.ReadLine()
	End Sub
End Module

Class Response
	Public Property [License]() As License
	Public Property Result() As Boolean
	Public Property Messages() As List(Of String)
End Class

Class License
	Public Property LicenseKey() As String
	Public Property LicenseID() As String
	Public Property Username() As String
	Public Property Password() As String
	Public Property ProductCode() As String
	Public Property ProductName() As String
	Public Property Quantity() As Integer
	Public Property PurchaseType() As String
	Public Property CreatedDate() As String
	Public Property ExpirationDate() As String
	Public Property CustomerName() As String
	Public Property CustomerCompany() As String
	Public Property CustomerEmail() As String
	Public Property DiscountCode() As Integer
	Public Property Status() As String
	Public Property BundleProductCode() As String
	Public Property BundleProductName() As String
	Public Property BundleQuantity() As Integer
	Public Property ELAPassword() As String
	Public Property CountryId() As Integer
End Class

Open in new window

Produces the following output -Capture.JPG-saige-
0
 

Author Closing Comment

by:jjvalstar
ID: 41857997
This was the solution.
Many thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now