baronming baronming - 26 days ago 4
Vb.net Question

vb.net Json Error converting value to type

I'm newbie in JSON, I made a first try with a file and it works fine. But my second file gives me headache.
The problem is that the second file start with this : [
my first file was like this

{
"GC": {
"Parameters": {
"feed": "gc",
"lang_code": "fr",
"fmt": "json",
} .................


I try many different things and I always have an error. The error is :
Additional information: Error converting value "person" to type 'JSonPerson'. Path '[0]', line 1, position 9.

Here's the second JSON file text (that start with the bracket "[" :

["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": ""
}]]


Now my class:

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


And my code (that is the same for the first file, works perfect, but not with the second file):

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)


I try this too:

Dim obj As JSonPerson
obj = JsonConvert.DeserializeObject(Of JSonPerson)(jsonData)


But I'm not able to push data inside my class I have errors:

Does someone know what's my problem ?

Update

I also tried reading directly from the stream using a
JsonTextReader
, like so:

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 have data. After the variable "reader" is empty. So the people is empty.

Answer

Here's the way I did it:

        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