Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to deserialise the following JSON text, but getting nowhere and I cant figure out where Im going wrong - can anyone point me in the right direction please?

{
"data":
{
"id":125,
"domain":"Sports",
"patient_name":"Fred Smith",
"patient_gender":"male",
"patient_image":"https:\/\/mysite.png",
"temperature":-105,
"duration":130,
"weir_height":160,
"session_rank":1,
"next_session_rank":2,
"next_session_date":
{
"date":"2017-10-15 10:00:00.000000",
"timezone_type":3,
"timezone":"UTC"
}
}
}

I think its something to do with the JSON fields being a sub-level of the 'data' key and that is what I'm finding hard to figure out how to deal with as the code works with JSON data that doesn't have this container.

What I have tried:

Using response As Net.HttpWebResponse = request.GetResponse
     Using streamReader As New IO.StreamReader(response.GetResponseStream)
     
     Dim jsonResponseText = streamReader.ReadToEnd
     Dim jsonResult As PFCNext = New Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonResponseText, GetType(PFCNext))
     
    End Using 'streamreader
End Using 'response

    Class PFCNext
        Property Id As String
        Property Domain As String
        Property Patient_name As String
        Property Patient_gender As String
        Property Patient_image As String
        Property Temperature As String
        Property Duration As String
        Property Weir_height As String
        Property Session_rank As String
        Property Next_session_rank As String
        Property Next_session_date As String
        Property Date_ As String
        Property Timezone_type As String
        Property Timezone As String
    End Class
Posted
Updated 6-Oct-17 13:22pm
v3

Unless you write a custom converter, the structure of your class needs to match the structure of your JSON:
VB.NET
Class PFCNextWrapper
    Public Property Data As PFCNext
End Class

Class PFCNext
    Public Property Id As Integer
    Public Property Domain As String
    Public Property Patient_name As String
    Public Property Patient_gender As String
    Public Property Patient_image As String
    Public Property Temperature As Integer
    Public Property Duration As Integer
    Public Property Weir_height As Integer
    Public Property Session_rank As Integer
    Public Property Next_session_rank As Integer
    Public Property Next_session_date As NextSessionDate
End Class

Class NextSessionDate
    Public Property [Date] As Date
    Public Property Timezone_type As Integer
    Public Property Timezone As String
End Class

You can then deserialize your JSON to the PFCNextWrapper type:
VB.NET
Dim serializer As New Web.Script.Serialization.JavaScriptSerializer()
Dim jsonResult As PFCNextWrapper = CType(serializer.Deserialize(jsonResponseText, GetType(PFCNextWrapper)), PFCNextWrapper)
 
Share this answer
 
Comments
Mehdi Gholam 6-Oct-17 14:04pm    
My 5!
SheepRustler 9-Oct-17 6:01am    
Perfect, thanks. The concept of having a sub class was new to me, so thanks for posting your solution it works perfectly.
This CodeProject article will show you how, with source code, to take raw JSON data, generate the classes quickly and deserialize the data: Working with JSON in C# & VB[^]
 
Share this answer
 
Comments
SheepRustler 9-Oct-17 6:01am    
Thanks, that's very useful.
Graeme_Grant 9-Oct-17 6:14am    
You are welcome. The article linked was put together after answering many questions, like yours, about working with JSON data, right here in the Q&A forums.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900