JSON double quote

Hi,

My JSON is like this

posts": [{ "id": 1, "type": "post", "slug": "post\_slug", "title": "Hello ” world “", }]

When I try to display the title using newText api. The text output the ” and “ which should be converted to quotes symbols " " 

How can I output the string converting both ” and “ with proper character? 

Well, I just did a quick & lazy solution

local title = posts[i].title title = title:gsub("%”", "\"") title = title:gsub("%“", "\"")

Hey Alyosef,

I suppose you are using json.decode() to retrieve the strings from the json file?

If yes, I think the problem is the escape sequence you are using.

As you can read in the JSON specs ( http://json.org/ ) special characters are used as the following:

\b Backspace (ascii code 08) \f Form feed (ascii code 0C) \n New line \r Carriage return \t Tab \" Double quote \\ Backslash caracter

Hope that helps :slight_smile:

@torbenratzlaff

Thank you for your answer. Yes, I am using json.decode() to retrieve the strings from the json file. You are right if I have control over what I am getting in the JSON file, then I can easily escape the special characters. 

Ah ok, so you got a JSON file that was meant for and uses escape sequences from another language (like HTML for example)?

Yes, you are correct. 

Well, I would try the following.

Use string.gsub (as you already did) but not again and again for every sequence you want to check for, instead use a table.

Additionally you use a pattern, to find the sequences you need.

This is explained in the docs ( https://docs.coronalabs.com/api/library/string/gsub.html ) but I will give it a short try:

local replacements = { ['”'] = '\"', ['“'] = '\"', } local pattern = "((%&#)%d+;)" function sanitizeString(text) return text:gsub(pattern, replacements) end

Hope that helps :slight_smile:

Thank you, it definitely helps. 

Well, I just did a quick & lazy solution

local title = posts[i].title title = title:gsub("%”", "\"") title = title:gsub("%“", "\"")

Hey Alyosef,

I suppose you are using json.decode() to retrieve the strings from the json file?

If yes, I think the problem is the escape sequence you are using.

As you can read in the JSON specs ( http://json.org/ ) special characters are used as the following:

\b Backspace (ascii code 08) \f Form feed (ascii code 0C) \n New line \r Carriage return \t Tab \" Double quote \\ Backslash caracter

Hope that helps :slight_smile:

@torbenratzlaff

Thank you for your answer. Yes, I am using json.decode() to retrieve the strings from the json file. You are right if I have control over what I am getting in the JSON file, then I can easily escape the special characters. 

Ah ok, so you got a JSON file that was meant for and uses escape sequences from another language (like HTML for example)?

Yes, you are correct. 

Well, I would try the following.

Use string.gsub (as you already did) but not again and again for every sequence you want to check for, instead use a table.

Additionally you use a pattern, to find the sequences you need.

This is explained in the docs ( https://docs.coronalabs.com/api/library/string/gsub.html ) but I will give it a short try:

local replacements = { ['”'] = '\"', ['“'] = '\"', } local pattern = "((%&#)%d+;)" function sanitizeString(text) return text:gsub(pattern, replacements) end

Hope that helps :slight_smile:

Thank you, it definitely helps.