读取 DataTable 时出现意外的 JSON 标记。预期的 StartArray,得到 StartObject。路径 '',第 1 行,位置 1
Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1
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
我得到了 json 但无法保存正文 Next
Json回复如下
{
"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"
}
得到JSON回应
您的 JSON 不完整。假设您的 JSON 应该如下所示:
{
"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"
}
]
}
}
}
需要通过response
--> body
--> price
访问得到数组
C# 语法
using Newtonsoft.Json.Linq;
JObject jObj = JObject.Parse(result);
DataTable dt = jObj["response"]["body"]["price"].ToObject<DataTable>();
VB.Net 语法
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>()
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
我得到了 json 但无法保存正文 Next
Json回复如下
{
"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"
}
得到JSON回应
您的 JSON 不完整。假设您的 JSON 应该如下所示:
{
"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"
}
]
}
}
}
需要通过response
--> body
--> price
访问得到数组
C# 语法
using Newtonsoft.Json.Linq;
JObject jObj = JObject.Parse(result);
DataTable dt = jObj["response"]["body"]["price"].ToObject<DataTable>();
VB.Net 语法
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>()