Deserializing an empty array using Json.NET
Asked Answered
S

3

5

I've got a C# application that uses Json.NET v7.0.1. As a result of a REST call, I get back some JSON in the form of:

{
"messages": [ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="},
              {"phoneNumber":"234-567-8901", "smsText":"efgh5678="},
              {"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ]
}

If there is no message data to return, the JSON that I get back looks like this:

{
"messages": [ ]
}

My Message class looks like this:

public class Message
{
    public string PhoneNumber { get; set; }
    public string SmsText { get; set; }

    public Message()
    {
    }

    public Message(string phoneNumber, string smsText)
    {
        PhoneNumber = phoneNumber;
        SmsText = smsText;
    }
}

When I try to deserialize the JSON, I'm using the following code:

Message[] messages = JsonConvert.DeserializeObject<Message[]>(json, new JsonSerializerSettings
{
    MissingMemberHandling = MissingMemberHandling.Ignore
});

The problem I'm running into is deserializing the JSON when the messages array is empty. When that occurs, I get the following exception:

Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'WindowsFormTestApplication.Message[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'messages', line 2, position 15.

When the JSON array is empty, I'd like my Message[] variable to be null. Do I need a custom converter to get this deserialized or am I missing something obvious? Thanks!

Sealey answered 20/8, 2015 at 15:40 Comment(1)
#25148672Presbyterial
B
7

You should either send just an array

[ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="},
              {"phoneNumber":"234-567-8901", "smsText":"efgh5678="},
              {"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ]

Or use some wrapper object like this

public WrapperObject 
{ 
    public Message[] messages { get; set; } 
}

And then deserialize using it

var wrapper = JsonConvert.DeserializeObject<WrapperObject>(json, new JsonSerializerSettings
    {
        MissingMemberHandling = MissingMemberHandling.Ignore
    });
Bioscope answered 20/8, 2015 at 15:50 Comment(0)
M
2

Create a wrapper class and deserialize the Json into it:

public class MessageArray
{
    public Message[] Messages;
}

var messages = JsonConvert.DeserializeObject<MessageArray>(json, new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Ignore
            });
Macri answered 20/8, 2015 at 16:21 Comment(0)
G
0

I got case where this time WraperObject dont help an I need to go for Message[] like this


var messages = JsonConvert.DeserializeObject<Message[]>(json, new JsonSerializerSettings
{
    MissingMemberHandling = MissingMemberHandling.Ignore
});

but its case when i get [] insted of [{...}]

Grandeur answered 19/2 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.