Home > Back-end >  Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path ''
Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path ''

Time:03-23

Unexpected JSON token when reading Data Table, Expected Start Array, got Start Object. Path '', line 1, position 1

Dim Data As String = "{" & " ""request"": { " & " ""header"": { " & " ""username"": ""MyUsername""," & " ""Password"": ""ogBc4Ceh"" " & "}," & " ""body"": {" & " ""shape"": ""round"" " & "}" & "}" & "}"
Dim URL As String = "https://technet.rapaport.com/HTTP/JSON/Prices/GetPriceSheet.aspx "
Dim webRequest As WebRequest = WebRequest.Create(URL)
webRequest.Method = "POST"
webRequest.ContentType = "application/x-www-form-urlencoded"
Dim reqStream As Stream = webRequest.GetRequestStream()
Dim postData As String = Data
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
reqStream.Write(postArray, 0, postArray.Length)
reqStream.Close()
Dim sr As StreamReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
Dim Result As String = sr.ReadToEnd()


Dim dt As DataTable = JsonConvert.DeserializeObject(Of DataTable)(Result)

For i As Integer = 0 To dt.Rows.Count - 1
    Dim constr As String = "Data Source=nikunj;Initial Catalog=DBFantasy;Integrated Security=True"
    Using conn As SqlConnection = New SqlConnection(constr)
        Dim sql As String = "INSERT INTO rap VALUES(@shape, @low_size,@high_size)"
        Using cmd As SqlCommand = New SqlCommand(sql, conn)
            cmd.Parameters.AddWithValue("@shape", dt.Rows(i)("shape").ToString())
            cmd.Parameters.AddWithValue("@low_size", dt.Rows(i)("low_size").ToString())
            cmd.Parameters.AddWithValue("@high_size", dt.Rows(i)("high_size").ToString())
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        End Using
    End Using
Next

I got json but can't save body Next

Json Response are as follows

{
"response":{
"header":{
"error_code":0,
"error_message":""
},

"body":{
"price":[
{
"shape":"round",
"low_size":0.01,
"high_size":0.03,
"color":"d",
"clarity":"if",
"caratprice":800,
"date":"2022-03-18"
},
{
"shape":"round",
"low_size":0.01,
"high_size":0.03,
"color":"d",
"clarity":"vvs1",
"caratprice":800,
"date":"2022-03-18"
}

Getting JSON Response

CodePudding user response:

Your JSON isn't complete. Assume that your JSON should be looked as below:

{
  "response": {
    "header": {
      "error_code": 0,
      "error_message": ""
    },
    "body": {
      "price": [
        {
          "shape": "round",
          "low_size": 0.01,
          "high_size": 0.03,
          "color": "d",
          "clarity": "if",
          "caratprice": 800,
          "date": "2022-03-18"
        },
        {
          "shape": "round",
          "low_size": 0.01,
          "high_size": 0.03,
          "color": "d",
          "clarity": "vvs1",
          "caratprice": 800,
          "date": "2022-03-18"
        }
      ]
    }
  }
}

You need to access through response --> body --> price to get the array.

C# syntax

using Newtonsoft.Json.Linq;

JObject jObj = JObject.Parse(result);
DataTable dt = jObj["response"]["body"]["price"].ToObject<DataTable>();

Sample program


VB.Net Syntax

Imports Newtonsoft.Json.Linq

Dim result As String = sr.ReadToEnd()

Dim jObj As JObject = JObject.Parse(result)
Dim dt As DataTable = jObj["response"]["body"]["price"].ToObject<DataTable>()

CodePudding user response:

You can copy your json result to here. And parse it , then you will find something error.

https://jsonformatter.curiousconcept.com/

  • Related