0

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.

8
  • 1
    You've been a member for almost 2 months, time to learn how to format code so it can be read. Commented Mar 2, 2017 at 19:46
  • Plutonix - yeah - was trying to remember how, answered someone else's question and saw the shortcut. Commented Mar 2, 2017 at 19:56
  • Look at my question - a little clarity here: Trying to get "value" out of the "response" area (what I think is a dictionary object) Commented Mar 2, 2017 at 20:02
  • I dont think that will deserialize as a Dictionary(Of String, String) because "responded_by": is more than just a string. Where did the classes come from? VS will build (mostly correct) ones for you Commented Mar 2, 2017 at 20:17
  • I built the classes, didn't know VS will build them for me (really new to JSON) will look into that. Commented Mar 2, 2017 at 20:27

1 Answer 1

0

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"))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I will try this. I didn't include the whole json file because it is pretty long.
It worked, the class structure is exactly what I needed. Need to figure out why I don't have the "Paste Special" in my VS 2015 install.
You have to have a class code file open first. Don't forget to mark the answer with the checkbox to the left.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.