1

Apologize if i am wrong, i have problem with with double quotes("") when encoding json string.

Brief: i have a string which identifies height

height = "5'2"" - 157 cm"; (5 feet 2 inches - 157 centimeters)

when i parse it through json_encode in PHP, it encoding fine but string displays as

"height":"5'2\"\" - 157 cm" 

after encoding in json string, i don't want the backslash(\) to be displayed in the front-end. So, how to remove the back slash(\). Help me. Thanks in Advance

3
  • 1
    You most definitely DO want the backslash there when the text is converted into JSON. Otherwise the JSON will be invalid. (Though it's not clear why you want the inch mark twice.) Commented Mar 4, 2012 at 13:38
  • \" is an escape sequence which tells the decoder (in which ever language) that " has to be taken literally (i.e. it is part of the string) and does not denote the end of the string value. Commented Mar 4, 2012 at 13:39
  • Note that this is the standard way of coding string literals in C, C++, Java, and a number of other programming languages. You need to understand the concept. Commented Mar 5, 2012 at 22:09

1 Answer 1

4

after encoding in json string, i don't want the backslash() to be displayed in the front-end.

The slashes are part of the JSON format, they are there by design and should not be removed needlessly.

They will disappear if you run json_decode() prior to displaying the data in the frontend.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, i am new to json so,you mean back slash will not be displayed after decoding.
@Rajneel: Not only in JSON, but everywhere you deal with strings, you will find escape sequences. Take PHP. If you run echo "foo"bar"; you will get an error, because PHP thinks the string is "foo" and then sees bar"; which is not valid PHP. echo "foo\"bar"; instead will work and output foo"bar. PHP knows then that the quote in the middle does not terminate the string but is part of the string. See also my comment to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.