Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NewtonSoft.JSON Deserialize – Not deserializing correctly. (VB.NET)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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 :-))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading