0

I'm trying to deserialize a JSON return however it either errors when I try to deserialize at the highest level, or doesn't error and just returns null values if I start deeper into the json.

I just get "null" returns for the rest of my deserialization - I have successfully deserialized messages separately but when I try to do the whole lot it just returns nulls to all my values.

It provides are error when I attempt to deserialize at response, I'm not clear what exactly is wrong.

Model


Public Class response
    
    public property http_code as string
    public property response_code as string
    public property response_msg as string
    public property data as smsdata
    
End Class

Public Class smsdata
    
    public property total_price as string 
    public property total_count as string
    public property queued_count as string
    public property messages as messages
    
End Class
Public Class messages
    
    Public property messages as list(of message)
    
End Class

Public Class message
    
    Public Property direction as string
    <JsonProperty("date")>
    Public property daterange as string
    <JsonProperty("to")>
    public property recipient as string
    public property body as string
    public property from as string
    public property schedule as string
    public property message_id as string
    public property message_parts as string
    public property message_price as string
    public property from_email as string
    public property list_id as string
    public property custom_string as string
    public property contact_id as string
    public property user_id as string
    public property subaccount_id as string
    public property country as string
    public property carrier as string
    public property status as string 
End Class```

Module Program
    Sub Main(args As String())
        
        dim jsonstring as string = "{ 'http_code': 200, 'response_code': 'SUCCESS', 'response_msg': 'Messages queued for delivery.', 'data': { 'total_price': 0.0516, 'total_count': 2, 'queued_count': 2, 'messages': [ { 'direction': 'out', 'date': 1637744634, 'to': '+44771111111111', 'body': 'test message, please ignore 1', 'from': 'Shield', 'schedule': 1637744634, 'message_id': 'A3F48CA8-79E6-416F-A8C6-7660BD7B7632', 'message_parts': 1, 'message_price': '0.0258', 'from_email': null, 'list_id': null, 'custom_string': '', 'contact_id': null, 'user_id': 100525, 'subaccount_id': 116285, 'country': 'GB', 'carrier': 'O2', 'status': 'SUCCESS' }, { 'direction': 'out', 'date': 1637744634, 'to': '+4477222222222', 'body': 'test message, please ignore 2', 'from': 'Clubcare', 'schedule': 1637744634, 'message_id': '670E96E2-ED4F-4FAB-8594-D49B244EA2C3', 'message_parts': 1, 'message_price': '0.0258', 'from_email': null, 'list_id': null, 'custom_string': '', 'contact_id': null, 'user_id': 100525, 'subaccount_id': 116285, 'country': 'GB', 'carrier': 'O2', 'status': 'SUCCESS' } ], '_currency': { 'currency_name_short': 'GBP', 'currency_prefix_d': '£', 'currency_prefix_c': 'p', 'currency_name_long': 'British Pounds' } } }"
        
        dim results = JsonConvert.DeserializeObject(Of response)(jsonstring)
        
    End Sub
End Module
1
  • @dba the single parentheses works - I have a working earlier example where I deserialize JUST the "messages" element. This is failing as I scale up. Example single parantheses. newtonsoft.com/json/help/html/deserializeobject.htm Commented Nov 24, 2021 at 11:43

1 Answer 1

1

Checking your code and the sample data:

Public Class smsdata
    
    public property total_price as string 
    public property total_count as string
    public property queued_count as string
    public property messages as List (Of message)
    
End Class

should do the trick and you can dump the Messages Class

(as of single qoutes: I never used them, but VS-Code mark them as error, however :-))

Sign up to request clarification or add additional context in comments.

1 Comment

This worked - also single quotes worked fine.

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.