Smith
Smith

Reputation: 3164

Json.Net - How to serialize a C# List without the outer brackets?

I'm using the SendGrid Api and apparently their API does not like an array of email messages to be passed in. It wants an array of objects without the outer brackets.

In their example http://www.newtonsoft.com/json/help/html/SerializingCollections.htm

You can see that the result of serializing a collection results in a Json Array of objects which makes perfect sense. But is there a way to omit the outer brackets or will I just have to parse them out of the string manually? I'd rather avoid parsing if possible.

Thanks.

Upvotes: 3

Views: 4430

Answers (1)

I doubt you'll find a serializer that does that because the behavior you're describing isn't valid JSON (see the format specification at http://www.json.org/), so by definition a correctly-implemented serializer won't do this. It's a little unclear to me exactly why you'd want to do this in the first place and I don't recommend doing it, but if you must you can always manually strip out the brackets using a regex or something.

Upvotes: 2

Related Questions