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.'
This exception was originally thrown at this call stack:
[External Code]
JsonData_Nwide.InsertNWJsonData() in Nwide.aspx.vb
JsonData_Nwide.btnProcess_Click(Object, System.EventArgs) in Nwide.aspx.vb
[External Code]
What I have tried:
My code
VB.NET
Public Class BouncesAndBlocks Public Property data As String 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 Private Sub InsertNWJsonData() Dim cnno As String = Me .txtCNNo.Text Dim url = New RestClient( " https://api.nationwide.com.my/api/track?cn_no=" & cnno) ' ServicePointManager.Expect100Continue = True ' ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType) ' url.Timeout = -1 Dim request = New RestRequest(Method.POST) request.AddHeader( " Authorization" , " Bearer 31|xxxxxxxxxxxxxxxxxxxxxxxxxx" ) 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) For Each bouncesAndBlock In res Dim conString As String = ConfigurationManager.ConnectionStrings( " ConnectStringAPI" ).ConnectionString Dim query As String = " INSERT INTO NationW_Data(cn_no, record_at, status_code, status_description, location_code, location_name, remark) " & " VALUES (@cn_no,@record_at,@status_code,@status_description,@location_code " & " @location_name,@remark)" Using con As SqlConnection = New SqlConnection(conString) Dim cmd As SqlCommand = New SqlCommand(query, con) cmd.Parameters.AddWithValue( " @cn_no" , bouncesAndBlock.cn_no) cmd.Parameters.AddWithValue( " @record_at" , bouncesAndBlock.record_at) cmd.Parameters.AddWithValue( " @status_code" , bouncesAndBlock.status_code) cmd.Parameters.AddWithValue( " @status_description" , bouncesAndBlock.status_description) cmd.Parameters.AddWithValue( " @location_code" , bouncesAndBlock.location_code) cmd.Parameters.AddWithValue( " @location_name" , bouncesAndBlock.location_name) cmd.Parameters.AddWithValue( " @remark" , bouncesAndBlock.remark) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Sub JSON:
JSON
{ " 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" : " " }]{ Public Property status As Boolean Public Property message As String Public Property data As List( Of BouncesAndBlocks) End Class Public Class BouncesAndBlocks Public Property cn_no As String Public Property record_at As DateTime 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
VB.NET
Dim result As Root = JsonConvert.DeserializeObject( Of Root)(json) Dim res As List( Of BouncesAndBlocks) = result.data
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •