Last post Oct 04, 2019 02:31 PM by ubique
Oct 03, 2019 09:34 AM|ubique|LINK
I have implemented a simple Web API using the Controller as below. There are 2 GET methods that return either a list/collection of
classItem or a single instance of classItem based on the classItem id field.
In my view class
public id as string
public name as string
....other class member data fields etc
In my controller
Public Class mobilController
' GET: api/mobil
Public Function GetValues() As Collection
dim oList as new Collection: dim item1 as new classItem: dim item2 as new classItem
item1.name="item1": item2.name ="item2"
' GET: api/mobil/5
Public Function GetValue(ByVal id As String) As classItem
dim item as new classItem
fnFillMyItemById(id, item) 'this call populates the item object with data based on the item id
When I run calls to the above GET methods using Postman then I can see the data come back - either as a collection of
2 item objects or as a single item object depending on which GET method I call.
My question: how do I recreate the data as either a collection or single class on the client side?
Do I need to use serialization ? Or what is the simplest way? I am using VB.
Any advice greatly appreciated.
Oct 04, 2019 06:26 AM|Fei Han - MSFT|LINK
If you'd like to consume APIs from your client application using VB, you can refer to the following code snippet.
Dim client As HttpClient = New HttpClient()
Dim result As String = client.GetStringAsync("http://xxx/api/mobil").Result
Dim classItem_list As List(Of classItem) = JsonConvert.DeserializeObject(Of List(Of classItem))(result)
'Dim result As String = client.GetStringAsync("http://xxx/api/mobil/5").Result
'Dim classItem As classItem = JsonConvert.DeserializeObject(Of classItem)(result)
Oct 04, 2019 02:31 PM|ubique|LINK
many thanks again for your guidance. I did not realise I had just discovered JSON object notation.
Now that you have show me the correct library calls to use, all is now much clearer.