0

I'm using Query Using HTTP GET on VB.Net to get a response with this code:

Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://api.site.com/api=xxxxxxxxxxxx&id=1")`

To view the string I use:

MsgBox(result)

The response I get is this:

{"result":"success","message":"Member found","member_data":{"member_id":"1","user_name":"John","first_name":"John","last_name":"Smith","password":"xxx","member_since":"2018-12-08","membership_level":"2","more_membership_levels":null,"account_state":"active","last_accessed":"2018-12-08 09:00:53","last_accessed_from_ip":"1.1.1.1","email":"john.smigh@test.com","phone":null,"address_street":null,"address_city":null,"address_state":null,"address_zipcode":null,"home_page":null,"country":null,"gender":"not specified","referrer":null,"extra_info":null,"reg_code":null,"subscription_starts":"2018-12-08","initial_membership_level":null,"txn_id":"","subscr_id":"","company_name":null,"notes":null,"flags":"0","profile_image":""}}

What I've been trying to figure out is how to get Name, Last Name, email, Membership level, and account state, into separate label.text, any help would be really appreciated!

Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • 1
    This is a JSON object. You can use an Online class generator for the JSON ([json2csharp](http://json2csharp.com/) or [QuickType](https://app.quicktype.io/#l=cs&r=json2csharp) for example, then convert the generated c# class(es) to VB.Net). Get the [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/) package. Use `JsonConvert.DeserializeObject()` to convert your JSON to a .Net class (or classes). – Jimi Dec 08 '18 at 14:41
  • Alright, that organized it a bit better, but how do i extract fields like Name, Last Name, email, Membership level, and account state to a separate labels? Should i use "for each line"? – Pasha Kovshik Dec 08 '18 at 14:48
  • If you use the [QuickType](https://app.quicktype.io/#l=cs&r=json2csharp) service, you'll also get the whole code required to deserialize (and serialize again) the JSON. Give it a look (set the name of main class as `Root` (for example) instead of the default weird `Welcome`). If you have specific errors or something doesn't work as espected, post back updating the question. – Jimi Dec 08 '18 at 14:51
  • Having problems with converting C# to vb.net, for some reason there are a lot of expression errors, and errors in general. – Pasha Kovshik Dec 08 '18 at 15:02
  • Ok seems like i found json to vb.net converter, pasted the code, no errors, but how do i make newtonsoft follow the rules i pasted from the converter? – Pasha Kovshik Dec 08 '18 at 15:14
  • There are many examples on SO: [How to convert properly an array in json?](https://stackoverflow.com/questions/34252853/how-to-convert-properly-an-array-in-json?answertab=active#tab-top) -- [Get value of JSON object in VB.net](https://stackoverflow.com/questions/20924190/get-value-of-json-object-in-vb-net?answertab=active#tab-top) -- [JSON.Net Cannot deserialize the current JSON array](https://stackoverflow.com/questions/40809394/json-net-cannot-deserialize-the-current-json-array?answertab=active#tab-top) and so on. – Jimi Dec 08 '18 at 15:51

1 Answers1

0

You need to deserialize JSON. You can use JavaScript(De)Serializer or something external such as newtonsoft.json (json.net). It's pretty much almost the same concept.

 Dim test As JObject = JObject.Parse(New WebClient().DownloadString("http://api.site.com/api=xxxxxxxxxxxx&id=1"))
 Dim test1 As JToken = test("member_data")
 Debug.Print(String.Format("{0}, {1}, {2}, {3}, {4}", test1("first_name"), test1("last_name"), test1("email"), test1("membership_level"), test1("account_state")))
 'output >>> John, Smith, john.smigh@test.com, 2, active

I used newtonsoft since I already have that in my current project.

It's like dictionary, just call the key(name) and you get your value.

CruleD
  • 1,153
  • 2
  • 7
  • 15