{"Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'id', line 1, position 9."}
{"Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'id', line 1, position 9."}
反序列化 json 到数据集时收到以下错误。
Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'id', line 1, position 9
Json 检索到:{"id":130,"type":"general","setup":"test?","punchline":"test."}
我的代码
Dim wc As New WebClient
Try
Dim res As String
For i = 0 To 5
res = wc.DownloadString("https://official-joke-api.appspot.com/random_joke")
Dim jObject = JsonConvert.DeserializeObject(res)
Dim ds As New DataSet
ds = JsonConvert.DeserializeObject(Of DataSet)(res)
MsgBox(ds.Tables.Count)
Next
Catch ex As Exception
MsgBox(ex)
End Try
您显示的JSON无效。 Datatable 需要 Object 数组,所以 JSON 应该如下所示:
[{"id":130,"type":"general","setup":"test?","punchline":"test."}]
作为用户 ,您可以给 Newtonsoft.Json 一个 class,命名为 Joke
,用于反序列化。您可以通过使用 <JsonProperty("nameInTheJson")>
.
修饰属性来解决 uppercase/lowercase 首字母命名约定的不匹配问题
与其将数据反序列化到数据集中的数据表中,您可以保持简单并创建一个 List(Of Joke)
.
这是一个控制台应用程序:
Imports System.Net
Imports Newtonsoft.Json
Module Program
Class Joke
<JsonProperty("id")>
Property Id As Integer
<JsonProperty("type")>
Property Type As String
<JsonProperty("setup")>
Property Setup As String
<JsonProperty("punchline")>
Property Punchline As String
End Class
Sub Main(args As String())
Dim jokes As New List(Of Joke)
Using wc As New WebClient()
For i = 1 To 5
Dim jokeInfo = wc.DownloadString("https://official-joke-api.appspot.com/random_joke")
jokes.Add(JsonConvert.DeserializeObject(Of Joke)(jokeInfo))
Next
End Using
' Alternative which fetches ten jokes in one go - note the use of (Of List(Of Joke))
'Using wc As New WebClient()
' Dim jokeInfo = wc.DownloadString("https://official-joke-api.appspot.com/jokes/ten")
' jokes = JsonConvert.DeserializeObject(Of List(Of Joke))(jokeInfo)
'End Using
For Each jk In jokes
Console.WriteLine($"{jk.Setup}{vbCrLf}{jk.Punchline}{vbCrLf}")
Next
End Sub
End Module
需要Using statement,因为WebClient在使用后需要处理掉以清理系统资源。 (您可以使用等效的 Try
...Finally
和 wc.Dispose()
。)
反序列化 json 到数据集时收到以下错误。
Unexpected JSON token when reading DataTable. Expected StartArray, got Integer. Path 'id', line 1, position 9
Json 检索到:{"id":130,"type":"general","setup":"test?","punchline":"test."}
我的代码
Dim wc As New WebClient
Try
Dim res As String
For i = 0 To 5
res = wc.DownloadString("https://official-joke-api.appspot.com/random_joke")
Dim jObject = JsonConvert.DeserializeObject(res)
Dim ds As New DataSet
ds = JsonConvert.DeserializeObject(Of DataSet)(res)
MsgBox(ds.Tables.Count)
Next
Catch ex As Exception
MsgBox(ex)
End Try
您显示的JSON无效。 Datatable 需要 Object 数组,所以 JSON 应该如下所示:
[{"id":130,"type":"general","setup":"test?","punchline":"test."}]
作为用户 Joke
,用于反序列化。您可以通过使用 <JsonProperty("nameInTheJson")>
.
与其将数据反序列化到数据集中的数据表中,您可以保持简单并创建一个 List(Of Joke)
.
这是一个控制台应用程序:
Imports System.Net
Imports Newtonsoft.Json
Module Program
Class Joke
<JsonProperty("id")>
Property Id As Integer
<JsonProperty("type")>
Property Type As String
<JsonProperty("setup")>
Property Setup As String
<JsonProperty("punchline")>
Property Punchline As String
End Class
Sub Main(args As String())
Dim jokes As New List(Of Joke)
Using wc As New WebClient()
For i = 1 To 5
Dim jokeInfo = wc.DownloadString("https://official-joke-api.appspot.com/random_joke")
jokes.Add(JsonConvert.DeserializeObject(Of Joke)(jokeInfo))
Next
End Using
' Alternative which fetches ten jokes in one go - note the use of (Of List(Of Joke))
'Using wc As New WebClient()
' Dim jokeInfo = wc.DownloadString("https://official-joke-api.appspot.com/jokes/ten")
' jokes = JsonConvert.DeserializeObject(Of List(Of Joke))(jokeInfo)
'End Using
For Each jk In jokes
Console.WriteLine($"{jk.Setup}{vbCrLf}{jk.Punchline}{vbCrLf}")
Next
End Sub
End Module
需要Using statement,因为WebClient在使用后需要处理掉以清理系统资源。 (您可以使用等效的 Try
...Finally
和 wc.Dispose()
。)