Solved

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

Posted on 2016-10-21
4
62 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 51

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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