We help IT Professionals succeed at work.

Deserialize JSON

FTX
FTX asked
on
Medium Priority
907 Views
Last Modified: 2012-08-11
Here is my data format and code to display values from the JSON variable.  This displays all of the value for attendees.  I need to be able to display each name, email value but get lost after this.

Dim cMessage As String = "{ ""meeting_id"": 1234, " & _
            """instance_id"": 5678," & _
            """attendees"": [ " & _
            "{" & _
            """name"": ""wesgroves"", " & _
            """email"": ""wes@wes.com"", " & _
            "}" & _
            "," & _
            "{" & _
            """name"": ""wes2groves"", " & _
            """email"": ""wes2@wes2.com"", " & _
            "}" & _
            "]" & _
            "}"


        Dim objResponse As JsonObject

        'MsgBox("Last response was: " + objResponse("error")("message"))

        objResponse = CType(JsonConvert.Import(cMessage), JsonObject)

        MsgBox("Last response was: " & objResponse("attendees").ToString)
Comment
Watch Question

Commented:
Ok, with the help of https://groups.google.com/forum/?fromgroups#!topic/jayrock/k2QIwLgDTYM[1-25] which actually takes you one level deeper into a JSON response, I answered my own question:

Dim cMessage As String = "{ ""meeting_id"": 1234, " & _
            """instance_id"": 5678," & _
            """attendees"": [ " & _
            "{" & _
            """name"": ""wesgroves"", " & _
            """email"": ""wes@wes.com"", " & _
            "}" & _
            "," & _
            "{" & _
            """name"": ""wes2groves"", " & _
            """email"": ""wes2@wes2.com"", " & _
            "}" & _
            "]" & _
            "}"

        Dim root As IDictionary = JsonConvert.Import(cMessage)

        Dim meetingid As String = root("meeting_id").ToString
        MsgBox("meeting_id: " & meetingid)

        Dim instanceid As String = root("instance_id").ToString
        MsgBox("instance_id: " & instanceid)

        Dim attendees As IList = root("attendees")
        For Each attendee As IDictionary In attendees
            MsgBox("name: " & attendee("name"))
            MsgBox("email" & attendee("email"))
        Next

Explore More ContentExplore courses, solutions, and other research materials related to this topic.