Solved

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

Posted on 2016-10-21
4
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 52

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 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Calendar Control 5 54
Shared Service Environment 2 54
Subtract dates in vb.net 6 34
Why am I losing my session variable value 11 39
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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