Hi
I have an issue in stored the Json Data into database
Please advice me
Thank you
Maideen
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
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)
Dim request = New RestRequest(Method.POST)
request.AddHeader("Authorization", "Bearer 31|xxxxxxxxxxxxxxxxxxxxxxxxxx")
Dim response As IRestResponse = url.Execute(request)
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
Next
End Sub
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":""}]{