简体   繁体   中英

Deserializing json with nested dictionary

I've gotten pretty far but ran into wall with this. Have JSON coming from a web API I am using.

Here is the Json string (or at least the first part)

{
  "id": "065f1b17-0b2c-47c1-9674-c2bfda8c05bc",
  "number": "167",
  "title": "SSF-5003 MC Checklist HVAC",
  "status": "open",
  "description": null,
  "shared": false,
  "items": 
  [
    {
      "id": "a31a2f92-e948-4563-809e-6faed67e5db3",
      "description": "Project Number",
      "type": "text-area",
      "comment": null,
      "issue": null,
      "response": {
        "value": "Az001",
        "responded_by": {
          "id": "1208055465",
          "organization": {
            "id": "1207960635",
            "name": "xxxxxxxxx",
            "trading_name": "xxxx"
          },
          "first_name": "Brett",
          "last_name": "VanDyke"
        },
        "responded_at": "2017-03-02T14:45:47.924Z"
      },
      "item_number": "1",
      "photo_url": null,
      "response_options": [ ]
    },

My two classes:

Public Class HVAC
    Public Property id As String
    Public Property number As Int64
    Public Property title As String
    Public Property status As String
    Public Property description As String
    Public Property items As hvac_item()
End Class

Public Class hvac_item
    Public Property id As String
    Public Property description As String
    Public Property type As String
    Public Property item_number As Int32
    Public Property response As Dictionary(Of String, String)
End Class

Main code deserliazing the json:

json_in = File.ReadAllText(Path.GetTempPath() & "\hvac.json")
Dim hvac = JsonConvert.DeserializeObject(Of HVAC)(json_in)

If I comment out the "response" property I deserialize without any errors and see all info (except in the "response" section).

Cannot figure out how to get the dictionary information out. Think I might have to deserialize that within the hvac_item class but not sure.

Any help would be greatly appreciated.

These are my best guess at the classes I get from the incomplete JSON fragment provided...

Public Class HVAC
    Public Property id As String
    Public Property number As String
    Public Property title As String
    Public Property status As String
    Public Property description As Object
    Public Property _shared As Boolean
    Public Property items As Item()
End Class

Public Class Item
    Public Property id As String
    Public Property description As String
    Public Property type As String
    Public Property comment As Object
    Public Property issue As Object
    Public Property response As Response
    Public Property item_number As String
    Public Property photo_url As Object
    Public Property response_options As Object()
End Class

Public Class Response
    Public Property value As String
    Public Property responded_by As Responded_By
    Public Property responded_at As Date
End Class

Public Class Responded_By
    Public Property id As String
    Public Property organization As Organization
    Public Property first_name As String
    Public Property last_name As String
End Class

Public Class Organization
    Public Property id As String
    Public Property name As String
    Public Property trading_name As String
End Class

And to access the data....

Dim HVAC = Newtonsoft.Json.JsonConvert.DeserializeObject(Of HVAC)(IO.File.ReadAllText("\HVAC.json"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM