Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
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
    Next



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":""}]{
Posted
Updated 6-Jul-21 5:58am
v2

Your class structure doesn't match your JSON.
VB.NET
Public Class Root
    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
 
Share this answer
 
v2
Comments
Richard MacCutchan 6-Jul-21 12:28pm    
Is status a string or a bool?
Richard Deeming 6-Jul-21 12:30pm    
It looks like it's a bool, but JSON.NET should be able to deserialize it to either a string or a boolean.
Richard MacCutchan 6-Jul-21 12:45pm    
Thanks, As mentioned below, I am not familiar with the NewtonSoft implementation.
You are trying to convert a single name/value pair into a List:
{"status":true,"message":"Success",

You need to process that field first and use the text following the data key as the List.
 
Share this answer
 
Comments
Maideen Abdul Kader 6-Jul-21 5:37am    
thank you for prompt reply. But how to process first. pls can you explain.
Richard MacCutchan 6-Jul-21 6:15am    
Sorry, I don't know NewtonSoft. But I assume that you need to convert the first elements to simple types: bool and string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900