Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

622 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