Hi
I have issue in
Newtonsoft.Json.JsonSerializationException
Once use the external api url, error prompt
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
Pls advice me.
Thank you
Maideen
What I have tried:
Public Class BouncesAndBlocks
Public Property status As String
Public Property message As String
Public Property data As IEnumerable(Of Data)
End Class
Public Class Data
Public Property sname As String
Public Property saddress1 As String
Public Property saddress2 As String
Public Property scity As String
Public Property spostcode As String
Public Property sstate As String
Public Property dairport As String
Public Property scountry As String
Public Property detailOfContent As String
Public Property weight As String
Public Property productPackages As String
Public Property dimensionX As String
Public Property dimensionY As String
Public Property dimensionZ As String
Public Property chargeableRate As String
Public Property rname As String
Public Property rphone As String
Public Property raddress1 As String
Public Property raddress2 As String
Public Property rpostcode As String
Public Property rcity As String
Public Property rstate As String
Public Property rairport As String
Public Property rcountry As String
Public Property rremark As String
Public Property rvia As String
Public Property serviceModeId As String
Public Property agentId As String
Public Property trackingNo As String
End Class
Private Sub POST_TEST_DATA()
Dim url = New RestClient("http://avalon.insignianetworks.cloud/srcargo/rest/getBookingList")
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
url.Timeout = -1
'Dim request = New RestRequest(Method.POST)
Dim request = New RestRequest(Method.GET)
request.AddHeader("Authorization", "Bearer 123|ABC123")
Dim response As IRestResponse = url.Execute(request)
Dim json As String = response.Content
Dim res As BouncesAndBlocks = JsonConvert.DeserializeObject(Of BouncesAndBlocks)(json) '' original
For Each bouncesAndBlock In res.data
Try
Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectString").ConnectionString
Dim query As String = "INSERT INTO Red_X_temp " &
"(sname, saddress1, saddress2, scity, spostcode, sstate," &
"dairport, scountry, detailOfContent, weight, productPackages, " &
"dimensionX, dimensionY, dimensionZ, chargeableRate, rname, rphone, " &
"raddress1, raddress2, rpostcode, rcity, rstate, rairport, rcountry, " &
"rremark, rvia, serviceModeId, agentId, trackingno)" &
"VALUES" &
"(@sname, @saddress1, @saddress2, @scity, @spostcode, @sstate," &
"@dairport, @scountry, @detailOfContent, @weight, @productPackages, " &
"@dimensionX, @dimensionY, @dimensionZ, @chargeableRate, @rname, @rphone, " &
"@raddress1, @raddress2, @rpostcode, @rcity, @rstate, @rairport, @rcountry, " &
"@rremark, @rvia, @serviceModeId, @agentId, @trackingno)"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@sname", bouncesAndBlock.sname)
cmd.Parameters.AddWithValue("@saddress1", bouncesAndBlock.saddress1)
cmd.Parameters.AddWithValue("@saddress2", bouncesAndBlock.saddress2)
cmd.Parameters.AddWithValue("@scity", bouncesAndBlock.scity)
cmd.Parameters.AddWithValue("@spostcode", bouncesAndBlock.spostcode)
cmd.Parameters.AddWithValue("@sstate", bouncesAndBlock.sstate)
cmd.Parameters.AddWithValue("@dairport", bouncesAndBlock.dairport)
cmd.Parameters.AddWithValue("@scountry", bouncesAndBlock.scountry)
cmd.Parameters.AddWithValue("@detailOfContent", bouncesAndBlock.detailOfContent)
cmd.Parameters.AddWithValue("@weight", bouncesAndBlock.weight)
cmd.Parameters.AddWithValue("@productPackages", bouncesAndBlock.productPackages)
cmd.Parameters.AddWithValue("@dimensionX", bouncesAndBlock.dimensionX)
cmd.Parameters.AddWithValue("@dimensionY", bouncesAndBlock.dimensionY)
cmd.Parameters.AddWithValue("@dimensionZ", bouncesAndBlock.dimensionZ)
cmd.Parameters.AddWithValue("@chargeableRate", bouncesAndBlock.chargeableRate)
cmd.Parameters.AddWithValue("@rname", bouncesAndBlock.rname)
cmd.Parameters.AddWithValue("@rphone", bouncesAndBlock.rphone)
cmd.Parameters.AddWithValue("@raddress1", bouncesAndBlock.raddress1)
cmd.Parameters.AddWithValue("@raddress2", bouncesAndBlock.raddress2)
cmd.Parameters.AddWithValue("@rpostcode", bouncesAndBlock.rpostcode)
cmd.Parameters.AddWithValue("@rcity", bouncesAndBlock.rcity)
cmd.Parameters.AddWithValue("@rstate", bouncesAndBlock.rstate)
cmd.Parameters.AddWithValue("@rairport", bouncesAndBlock.rairport)
cmd.Parameters.AddWithValue("@rcountry", bouncesAndBlock.rcountry)
cmd.Parameters.AddWithValue("@rremark", bouncesAndBlock.rremark)
cmd.Parameters.AddWithValue("@rvia", bouncesAndBlock.rvia)
cmd.Parameters.AddWithValue("@serviceModeId", bouncesAndBlock.serviceModeId)
cmd.Parameters.AddWithValue("@agentId", bouncesAndBlock.agentId)
cmd.Parameters.AddWithValue("@trackingno", bouncesAndBlock.trackingNo)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert('Record Saved !');", True)
Catch ex As Exception
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert('Record wrong !');", True)
End Try
Next
End Sub
I have debugged, I have got below Json which is from external API url.
this is that
[
{
""id"":120,
""trackingNo"":""683123690493"",
""sname"":""AMAN SALES SDN BHD"",
""sphone"":""019-8824500"",
""saddress1"":""NO 39/39A,JALAN 23 TAMAN"",
""saddress2"":""BUKIT KUCHAI"",
""scity"":""PUCHONG"",
""spostcode"":""47100"",
""sstate"":""Selangor"",
""dairport"":""KUL"",
""scountry"":""MY"",
""detailOfContent"":""PARTS"",
""weight"":5.0,
""productPackages"":1,
""dimensionX"":40.0,
""dimensionY"":28.0,
""dimensionZ"":26.0,
""chargebleRate"":5.0,
""rname"":""AMAN SALES & SERVICES (M) SDN BHD"",
""rphone"":""089-880757"",
""raddress1"":""LOT13 NEW KIMBELL INDUSTRIAL"",
""raddress2"":""ESTATE,MDLD 4974,JALAN KIMBELL 91100"",
""rpostcode"":""91100"",
""rcity"":""LAHAD DATU"",
""rstate"":""Sabah"",
""rairport"":""LDU"",
""rcountry"":""MY"",
""rremark"":""PLEASE DEL ASAP"",
""rvia"":""BKI"",
""serviceModeId"":null,
""agentId"":null"
}
]