Mount RegEx to change string json

1

I have the following string, which will actually be treated as a json , stored in a column in the database in my SQL Server :

{"PT":"adssadsadsd "asdada"","ES":"","FR":"","EN":""}

How do I automatically change the string to look like this:

{"PT":"adssadsadsd \"asdada\"","ES":"","FR":"","EN":""}

I want to make this change in the Controller of my MVC project. I need this because I have records in the database that are poorly formatted and then I can not fetch the information in the correct way.

    
asked by anonymous 01.03.2017 / 16:52

1 answer

0

Montei in Fiddle with an idea . It's not exactly the best, but it's based on the idea that if I change the initial quotation marks by apostrophes and serialize, the deserializer JSON will understand that the quotation mark is part of the field, not a delimiter.

public static IEnumerable<String> RetirarAspas(string jsonString) 
{
    var regex = new Regex("(\")([\w]+)(\")(:)(\")([\w\s\"]*)(\"),?");
    var teste = regex.Matches(jsonString);

    foreach (Match match in teste) 
    {
        String linha = "";

        for (int i = 1; i < match.Groups.Count; i++) 
        {
            if (match.Groups[i].ToString() == "\"") 
                linha += "'";
            else
                linha += match.Groups[i].ToString();
        }

        yield return linha;
    }
}

Then I can mount JSON like this:

    var testeJson = "{";
    foreach (var linha in lista)
    {
        Console.WriteLine(linha);
        testeJson += linha + ", ";
    }

    testeJson += "}";

    var jsonDesserializado = JsonConvert.DeserializeObject<dynamic>(testeJson);
    Console.Write(jsonDesserializado);
    
01.03.2017 / 18:01