我是JSON的新手,我第一次尝试使用文件,它运行正常。但我的第二个档案让我很头疼。 问题是第二个文件以此开头:[ 我的第一个文件是这样的
{
"GC": {
"Parameters": {
"feed": "gc",
"lang_code": "fr",
"fmt": "json",
} .................
我尝试了很多不同的事情,但我总是有错误。错误是: 附加信息:将值“person”转换为“JSonPerson”类型时出错。路径'[0]',第1行,第9位。
这是第二个JSON文件文本(以括号“[”:
开头)["person",
[{
"id": "19023",
"player_id": "16493",
"coach_id": "0",
"manager_id": "",
"official_id": ""
},
{
"id": "19024",
"player_id": "16494",
"coach_id": "1",
"manager_id": "",
"official_id": ""
}]]
现在我的班级:
Public Class JSonPerson
Public person As List(Of JSon_PersonDetail)
End Class
Public Class JSon_PersonDetail
Public id As String
Public player_id As String
Public coach_id As String
Public manager_id As String
Public official_id As String
Public user_id As String
Public first_name As String
Public last_name As String
End Class
我的代码(对于第一个文件来说是相同的,完美的,但不是第二个文件):
Dim client As New WebClient()
Dim stream As Stream = client.OpenRead("c:\Global_files\person.json")
Dim reader2 As New StreamReader(stream)
Dim jsonData As String = reader2.ReadToEnd
Dim obj As List(Of JSonPerson)
obj = JsonConvert.DeserializeObject(Of List(Of JSonPerson))(jsonData)
我也试过这个:
Dim obj As JSonPerson
obj = JsonConvert.DeserializeObject(Of JSonPerson)(jsonData)
但是我无法在课堂上推送数据我有错误:
有人知道我的问题是什么吗?
更新
我也尝试使用JsonTextReader
直接从流中读取,如下所示:
Dim client As New WebClient()
Dim stream As Stream = client.OpenRead("c:Global_files\person.json")
Dim reader2 As New StreamReader(stream)
Dim jsonData As String = reader2.ReadToEnd
Dim reader As New JsonTextReader(reader2)
Dim people As JSonPeople
people = JsonSerializer.CreateDefault().Deserialize(Of JSonPeople)(reader)
reader2有数据。变量“reader”为空后。所以人们都是空的。
答案 0 :(得分:0)
这就是我做的方式:
Dim client As New WebClient()
Dim stream As Stream = client.OpenRead("c:\Global_files\person.json")
Dim reader2 As New StreamReader(stream)
Dim jsonData As String = reader2.ReadToEnd
Dim jResults As JArray = JArray.Parse(jsonData)
'Here I know that is the children #1 that I need
Dim Data As List(Of JToken) = jResults.Item(1).Children().ToList
Dim vListOfPerson As New List(Of JSon_PersonDetail)
For Each item In Data
Dim ItemArray As Array = item.ToArray
Dim person As New JSon_PersonDetail
For Each element In ItemArray
Select Case element.name
Case "id"
person.id = element.value
Case "player_id"
person.player_id = element.value
Case "coach_id"
person.coach_id = element.value
Case "manager_id"
person.manager_id = element.value
Case "official_id"
person.official_id = element.value
Case "user_id"
person.user_id = element.value
Case "first_name"
person.first_name = element.value
Case "last_name"
person.last_name = element.value
End Select
Next
vListOfPerson.Add(person)
Next