Solved

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

Posted on 2016-10-21
4
41 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

20 Experts available now in Live!

Get 1:1 Help Now