Best ways to split a string with matching curly braces

You can use a JsonTextReader with the SupportMultipleContent flag set to true to read this non-standard JSON. Assuming you have a class Person that looks like this:

class Person
{
    public string Name { get; set; }
}

You can deserialize the JSON objects like this:

string json = @"{""name"": ""John""}{""name"": ""Joe""}";

using (StringReader sr = new StringReader(json))
using (JsonTextReader reader = new JsonTextReader(sr))
{
    reader.SupportMultipleContent = true;

    var serializer = new JsonSerializer();
    while (reader.Read())
    {
        if (reader.TokenType == JsonToken.StartObject)
        {
            Person p = serializer.Deserialize<Person>(reader);
            Console.WriteLine(p.Name);
        }
    }
}

Fiddle: https://dotnetfiddle.net/1lTU2v

Leave a Comment