1

I am consuming a web service that is sending multiple objects in a json string.

{  "id": null,  "method": "ready",  "params": [    {      "accept": 1,      "serial": "001d50101979"    },
{
  "result": {
    "serial": "001d50101979",
    "name": "001d50101979",
    "model": "HMP200",
    "mode": "normal",
    "firmware": {
      "version": "3.2.2-1.0.28801",
      "status": "normal"
    },
    "uptime": "233.50",
    "bootid": "e62f7839-95b1-4775-8476-c0b1b5b4857f"
  },
  "error": null,
  "id": 1231
}  ] }

I am using the following classes

Public Class Firmware
   Public Property version As String
   Public Property status As String
End Class
Public Class Result
   Public Property serial As String
   Public Property name As String
   Public Property model As String
   Public Property mode As String
   Public Property firmware As Firmware
   Public Property uptime As String
   Public Property bootid As String
End Class
Public Class Param
   Public Property accept As Integer
   Public Property serial As String
End Class
Public Class Player
   Public Property id As Object
   Public Property method As String
   Public Property params As Param()
End Class

I have no issue deserializing the root class Player but I am not sure how to deserialize the class Result.

 Dim Player As New Player
 Player = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Player)(JsonString)
 Dim FirmwareVersion As String = Player.id
 Dim bootid As String = Player.method
 Dim Model As String = Player.params(0).accept
 Dim Serial As String = Player.params.ElementAt(0).serial
3
  • The Param class is wrong. It should contain Result. Run your json through something like json2csharp.
    – Jim Hewitt
    Jul 11, 2016 at 4:16
  • Param and result are being passed as two separate separate classes. The Param class is included in the Player class and I can access it. The Result class is not included in the Player classes and I am not sure how to access it.
    – rick9rick
    Jul 11, 2016 at 4:20
  • When I run the json though a json to C# I get the following classes
    – rick9rick
    Jul 11, 2016 at 4:24

1 Answer 1

2

Change your class Param to

Public Class Param
    Public Property accept As Integer
    Public Property serial As String
    Public Property result As Result
End Class

then you can access your result like so

Dim Player As New Player
Player = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Player)(JsonString)
Dim FirmwareVersion As String = Player.id
Dim bootid As String = Player.method
Dim Model As String = Player.params(0).accept
Dim Serial As String = Player.params.ElementAt(0).serial

For Each p In Player.params
    If p.result IsNot Nothing Then
        Console.WriteLine(p.result.model)
    End If
Next
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.