0

I am just trying to figure out the best way to deserialize a json string returned from a 3rd party api call. I read ServiceStack is fast so want to try it out. No experience and here is what I have done:

  1. Opened Visual Studio 2013
  2. Created new project Windows Forms Application
  3. Installed ServiceStack.Text (based on https://servicestack.net/download)
  4. Added a button (btnView) and textbox (txtOutput)
  5. Add code to btnView_Click event

           Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
    
        Me.Cursor = Cursors.WaitCursor
    
        Dim wp As New WebPost 'this allows to pass url and return results
        wp.URL = "xxxx"
        Dim sJSONRetVal As String = wp.Request(String.Empty, True)
    'sJSONRetVal return values looks like the following:
    '{"complaints":[{"feedback_type":"abuse","subject":"Sales Agent Position"},{"feedback_type":"abuse","subject":"Sales Agent Position"}],"message":"OK","code":0}
    
    
        'ServiceStack.Text example
        Dim t As SMTP_Complaints = ServiceStack.Text.JsonSerializer.DeserializeFromString(Of SMTP_Complaints)(sJSONRetVal)
    
        'For Each xi As SMTP_Complaints In t
        '    txtOutput.Text &= xi.mail_from & vbCrLf
        'Next
    
        wp = Nothing
    
        txtOutput.Text = t.ToString
    
        Me.Cursor = Cursors.Default
    
    End Sub
    
    Public Class SMTP_Complaints
    
    Dim _feedback_type As String = ""
    Dim _subject As String = ""
    
    Public Property feedback_type As String
        Get
            Return _feedback_type
        End Get
        Set(value As String)
            _feedback_type = value
        End Set
    End Property
    
    Public Property subject As String
        Get
            Return _subject
        End Get
        Set(value As String)
            _subject = value
        End Set
    End Property
    End Class
    

The above doesn't seem to get any data. how would I loop through the data returned and return the data from both instances? Just not sure how I need to set this up to read the json data and then be able to output.

lleemon
  • 55
  • 1
  • 8

1 Answers1

3

Based on the returned JSON of:

{"complaints":[{"feedback_type":"abuse","subject":"Sales Agent Position"},{"feedback_type":"abuse","subject":"Sales Agent Position"}],"message":"OK","code":0}

You will need two DTOs to deserialise this result.

I have used auto implemented properties here to simplify the complexity of the code. If you use an older version of VB, you'll need to expand these out to include a backing field with get and set method.

Public Class SMTP_Complaint
    Public Property feedback_type As String
    Public Property subject As String
End Class

Public Class SMTP_ComplaintsResponse
    Public Property complaints As SMTP_Complaint()
    Public Property message As String
    Public Property code As Integer
End Class

You need the SMTP_ComplaintsResponse class because your complaints are wrapped in your JSON response.

Then to deserialise the response:

Dim response = JsonSerializer.DeserializeFromString(Of SMTP_ComplaintsResponse)(sJSONRetVal)

And your complaints are then accessible:

For Each complaint As var In response.complaints
    Console.WriteLine("Type: {0}, Subject {1}", complaint.feedback_type, complaint.subject)
Next
Scott
  • 21,211
  • 8
  • 65
  • 72