Soham Dasgupta
Soham Dasgupta

Reputation: 5199

Remove illegal character from JSON before deserializing with JSON.NET

I have a string like which I want deserialize to datatable. Because it has a double quote in the value JSON.NET is not able to deserialize this.

[{"name":"soham "the" dasgupta"}]

Now I would like to replace the "the" with the. Please help. Replace("\"", "") is not working obviously.

Upvotes: 2

Views: 2842

Answers (1)

mrx
mrx

Reputation: 46

Basically you need to find the characters between the quotes and check if they are valid JSON delimiters.

I've wrote a quick and dirty function which searches the substrings between the strings and matches them with valid JSON delimiters. If the substring don't match, it escapes the quotes.

Note: This code is an very basic example of my idea, it could possibly not deal with every kind of JSON input.

static string FixJson(string input)
{
    var output = input;
    for (var x = 0; x < input.Length; x++)
    {
        if (input[x] != '\"') continue;

        for (var y = x + 1; y < input.Length; y++)
        {
            if (input[y] != '\"') continue;

            var found = false;
            for (var z = y + 1; z < input.Length; z++)
            {
                if (input[z] != '\"') continue;

                var tmp = input.Substring(y + 1, z - y - 1);
                if (tmp.Any(t => t != ' ' && t != ':' && t != ',' && t != '{' && t != '}'))
                {
                    output = output.Replace("\"" + tmp + "\"", "\\\"" + tmp + "\\\"");
                }

                x = y;
                found = true;
                break;
            }

            if (found)
                break;
        }
    }

    return output;
}

Upvotes: 3

Related Questions