Hi Sir,
I have used below link by Mr. dharmendr previously and also using now for API and web service is working well.
But some url cannot work and prompt error.
Newtonsoft.Json.JsonSerializationException:
'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[JsonData_Nwide+BouncesAndBlocks]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'status', line 1, position 10.'
The json format like this
{"status":true,"message":"Success",
"data":
[{"cn_no":"R3151248","record_at":"2020-12-24 23:39:37","status_code":"POD","status_description":"Delivered to","location_code":"JHB","location_name":"JOHOR BAHRU","remark":"SYAZWANI"},
{"cn_no":"R3151248","record_at":"2020-12-24 09:05:19","status_code":"CKL","status_description":"Arrived at delivery branch ","location_code":"JHB","location_name":"JOHOR BAHRU","remark":""},
{"cn_no":"R3151248","record_at":"2020-12-24 00:41:24","status_code":"CKO","status_description":"Item dispatched out","location_code":"HUB","location_name":"HUB in Nationwide Shah Alam","remark":""},
{"cn_no":"R3151248","record_at":"2020-12-23 18:51:33","status_code":"CKI","status_description":"Item received","location_code":"NMB","location_name":"Nationwide Main Branch S Alam","remark":""}]}
my code
Public Class BouncesAndBlocks
Public Property data As List(Of Response)
Public Property status As String
Public Property message As String
Public Property cn_no As String
Public Property record_at As String
Public Property status_code As String
Public Property status_description As String
Public Property location_code As String
Public Property location_name As String
Public Property remark As String
End Class
Dim cnno As String = Me.txtCNNo.Text
Dim url = New RestClient("https://api.nationwide.com.my/api/track?cn_no=" & cnno)
url.Timeout = -1
Dim request = New RestRequest(Method.POST)
request.AddHeader("Authorization", "Bearer 31|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Dim response As IRestResponse = url.Execute(request)
'Console.WriteLine(response.Content)
Dim json As String = response.Content
Dim res As List(Of BouncesAndBlocks) =
JsonConvert.DeserializeObject(Of List(Of BouncesAndBlocks))(json)
I have debugged. error prompt once reach this line Dim res As List(Of BouncesAndBlocks) = JsonConvert.DeserializeObject(Of List(Of BouncesAndBlocks))(json)
Where I did wrong, I don't know.
Please advice me
Thank you
Maideen