-20

I have some URLs in my database, and I want to print them as JSON using PHP. How can I get the desired format, without escaping backslashes, in my JSON object?

// Business to get data ... // print JSON data echo json_encode($customer, JSON_UNESCAPED_UNICODE); 

Enter image description here

6
  • 1
    i think extra spaces are added n you want to remove that? Commented Mar 11, 2017 at 4:02
  • What's in the variable $customer? Commented Mar 11, 2017 at 4:41
  • 2
    This sounds like a typical xy problem. Why would you want to print the json unescaped? Javascript have a command for consuming JSON, it's called JSON.parse, and it allows you to use json and transform it into javascript objects. Commented Mar 11, 2017 at 16:03
  • Possible duplicate of json_encode() escaping forward slashes Commented Mar 12, 2017 at 0:58
  • hi @FélixGagnon-Grenier i will consume the json output in Android Project using java , i will not use javascript as a client Commented Mar 13, 2017 at 9:32

1 Answer 1

16

This sounds like a typical XY problem. There are very few situations where you would want to unescape JSON (and I say this to be polite, I can't think of a single one) and in your case, your code suggests you echo that JSON so it can be consumed elsewhere.

Elsewhere probably means on a client with JavaScript. There is the JSON.parse function that does exactly what you want, and more: it transforms JSON (correct JSON, that still has its escaping backslashes) into native JavaScript object.

echo json_encode(["url" => "http://stackoverflow.com"]); // {"url":"http:\/\/stackoverflow.com"} 

// out of scope, but response is the response from your ajax call var parsedJson = JSON.parse(response); console.log(parsedJson.url); // http://stackoverflow.com 

This is how you actually get your unescaped string from JSON.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.