NewtonJson.Parse with quotation marks in the middle

2

I have a column of type varchar in the database that takes a JSON. This column is the description of a company, but in 4 languages. The logic I have is as follows:

{  
    "PT":"Descrição com "aspas" quebra o meu código",  
    "ES":" ",  
    "FR":"fr ",  
    "EN":" en"  
}

How do I get JSON to be read correctly when I have quotation marks in the company description?

    
asked by anonymous 01.03.2017 / 16:10

2 answers

4

You need to "escape" the quotation marks that you have in your string using the backslash \ before the quotes.

{  
    "PT":"Descrição com \"aspas\" quebra o meu código",  
    "ES":" ",  
    "FR":"fr ",  
    "EN":" en"  
}
    
01.03.2017 / 16:49
2

You can use JToken and JObject of NewtonSoft itself.

using System;
using Newtonsoft.Json.Linq;

public class Program {
    public static void Main() {
        var text = "{\"SomeResponse\":{\"FIrstAttribute\":8,\"SecondAttribute\":\"On\",\"ThirdAttribute\":{\"Id\":2,\"FirstName\":\"Okkie\",\"Name\":\"Bokkie\",\"Street\":\"\",\"StreetNumber\":null,\"PostCode\":\"\",\"City\":\"\",\"Country\":\"}}}";
        var token = JToken.Parse(text);
        var json = JObject.Parse((string) token);
        Console.WriteLine(json);
    }
}

I placed it on GitHub for future reference .

Source of the Jon Skeet's answer in SO .

    
01.03.2017 / 16:58