Parse with quotation marks in the middle

Asked

Viewed 547 times

2

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

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

How I make JSON` read correctly when I have quotes in the company description?

2 answers

5


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

{  
    "PT":"Descrição com \"aspas\" quebra o meu código",  
    "ES":" ",  
    "FR":"fr ",  
    "EN":" en"  
}
  • That’s what I realized and have asked gurus how I can add this using regex in this question: http://answall.com/questions/187190/montar-regex-para-alterar-string-json

2

You can use the JToken and the JObject of Newtonsoft himself.

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 put in the Github for future reference.

Source of Jon Skeet’s response in the OS.

Since the . NET Core there is a best solution for JSON.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.