将 JSON 解析为 Json 对象时出现异常
Exception when parsing a JSON as a Json Object
我希望有人能帮我解决我哪里出错了。
我有一个简单的 JSON 文件,这是它的压缩版本。
[
{
"LocationGuid":"c35e549f-ffb8-4828-8290-d66c2f9b735f",
"Name":"Volkswagen",
"FolderPath":"C:\temp\Volkswagen"
},
{
"LocationGuid":"814e7584-9f99-40a6-8520-3f5d467ea8d9",
"Name":"Nissan",
"FolderPath":"C:\temp\Nissan"
},
{
"LocationGuid":"51d60458-01ac-421d-8fb8-3663f5963fbf",
"Name":"Ford",
"FolderPath":"C:\temp\Ford"
}
]
我想将数据分成几行。
以下代码编译...
Private Sub UpdateExMailLocations(sPath As String)
Dim sLocationGUID As String
Dim sName As String
Dim sFolderPath As String
'' Retrieve the whole JSON file
Dim rawJSON = File.ReadAllText(sPath)
'' Parse it into JSON items
Dim jsonObject As JObject = JObject.Parse(rawJSON)
Dim jsonArray As JArray = jsonObject("result")
For Each item As JObject In jsonArray
sLocationGUID = item.SelectToken("LocationGUID").ToString
sName = item.SelectToken("Name").ToString
sFolderPath = item.SelectToken("FolderPath").ToString
MsgBox("Record = " & sLocationGUID & "," & sName & "," & sFolderPath)
Next
End Sub
当它到达语句时:
Dim jsonObject As JObject = JObject.Parse(rawJSON)
它抛出以下异常:
Newtonsoft.Json.JsonReaderException: 'Error reading JObject from
JsonReader. Current JsonReader item is not an object: StartArray. Path
'', line 1, position 1.'
我是一个完全的新手,所以你可能需要用简单的术语来说明解决方案:-)
你JSON是用方括号([ ... ]
)括起来的,所以它的结构代表的是对象数组,而不是单个对象。该数组包含具有三个属性的对象:两个类型 String
,一个可以直接解析为 Guid.
您可以使用 JArray.Parse()
来解析 JSON:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Dim locArray = JArray.Parse(rawJSON)
For Each loc As JObject In locArray
Console.WriteLine(loc("LocationGuid"))
Console.WriteLine(loc("Name"))
Console.WriteLine(loc("FolderPath"))
Next
或者构建一个描述 JSON 对象的简单 class 模型,然后反序列化为 List (Of Class)
或数组,然后像往常一样处理这个集合。
这种形式经常被使用,因为它允许使用 .Net 属性而无需手动将 JSON 属性 名称写入字符串。
你不需要回到JSON找到你要找的属性,可能嵌套在更复杂的结构中并且可以避免打字或解释错误。
有在线免费服务可以将 JSON 转换为 .Net classes,如 JSON Utils (includes VB.Net as language) and JSON validators/formatters as JSON Formatter and Validator。
Public Class LocationData
Public Property LocationGuid As Guid
Public Property Name As String
Public Property FolderPath As String
End Class
' [...]
' Deserialize to a List(Of class) objects
Dim locationsData = JsonConvert.DeserializeObject(Of List(Of LocationData))(rawJSON)
' Or as an array of objects:
' Dim locationsData = JsonConvert.DeserializeObject(Of LocationData())(rawJSON)
For Each loc As LocationData In locationsData
Console.WriteLine(loc.LocationGuid)
Console.WriteLine(loc.Name)
Console.WriteLine(loc.FolderPath)
Next
我希望有人能帮我解决我哪里出错了。
我有一个简单的 JSON 文件,这是它的压缩版本。
[
{
"LocationGuid":"c35e549f-ffb8-4828-8290-d66c2f9b735f",
"Name":"Volkswagen",
"FolderPath":"C:\temp\Volkswagen"
},
{
"LocationGuid":"814e7584-9f99-40a6-8520-3f5d467ea8d9",
"Name":"Nissan",
"FolderPath":"C:\temp\Nissan"
},
{
"LocationGuid":"51d60458-01ac-421d-8fb8-3663f5963fbf",
"Name":"Ford",
"FolderPath":"C:\temp\Ford"
}
]
我想将数据分成几行。
以下代码编译...
Private Sub UpdateExMailLocations(sPath As String)
Dim sLocationGUID As String
Dim sName As String
Dim sFolderPath As String
'' Retrieve the whole JSON file
Dim rawJSON = File.ReadAllText(sPath)
'' Parse it into JSON items
Dim jsonObject As JObject = JObject.Parse(rawJSON)
Dim jsonArray As JArray = jsonObject("result")
For Each item As JObject In jsonArray
sLocationGUID = item.SelectToken("LocationGUID").ToString
sName = item.SelectToken("Name").ToString
sFolderPath = item.SelectToken("FolderPath").ToString
MsgBox("Record = " & sLocationGUID & "," & sName & "," & sFolderPath)
Next
End Sub
当它到达语句时:
Dim jsonObject As JObject = JObject.Parse(rawJSON)
它抛出以下异常:
Newtonsoft.Json.JsonReaderException: 'Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.'
我是一个完全的新手,所以你可能需要用简单的术语来说明解决方案:-)
你JSON是用方括号([ ... ]
)括起来的,所以它的结构代表的是对象数组,而不是单个对象。该数组包含具有三个属性的对象:两个类型 String
,一个可以直接解析为 Guid.
您可以使用 JArray.Parse()
来解析 JSON:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Dim locArray = JArray.Parse(rawJSON)
For Each loc As JObject In locArray
Console.WriteLine(loc("LocationGuid"))
Console.WriteLine(loc("Name"))
Console.WriteLine(loc("FolderPath"))
Next
或者构建一个描述 JSON 对象的简单 class 模型,然后反序列化为 List (Of Class)
或数组,然后像往常一样处理这个集合。
这种形式经常被使用,因为它允许使用 .Net 属性而无需手动将 JSON 属性 名称写入字符串。
你不需要回到JSON找到你要找的属性,可能嵌套在更复杂的结构中并且可以避免打字或解释错误。
有在线免费服务可以将 JSON 转换为 .Net classes,如 JSON Utils (includes VB.Net as language) and JSON validators/formatters as JSON Formatter and Validator。
Public Class LocationData
Public Property LocationGuid As Guid
Public Property Name As String
Public Property FolderPath As String
End Class
' [...]
' Deserialize to a List(Of class) objects
Dim locationsData = JsonConvert.DeserializeObject(Of List(Of LocationData))(rawJSON)
' Or as an array of objects:
' Dim locationsData = JsonConvert.DeserializeObject(Of LocationData())(rawJSON)
For Each loc As LocationData In locationsData
Console.WriteLine(loc.LocationGuid)
Console.WriteLine(loc.Name)
Console.WriteLine(loc.FolderPath)
Next