0

I have created a webservice and return data in json but on API testing site showing response not in formatted form.

My code is as below :

return json_encode(array("Data" => array("data" => "hello","data2" => "hello1")),JSON_PRETTY_PRINT);

enter image description here

How to get it?

3
  • What's wrong with the regular output? But I guess you could set the JSON_PRETTY_PRINT flag. secure.php.net/json_encode Commented Jun 22, 2016 at 12:58
  • Already tried JSON_PRETTY_PRINT, but not working. Commented Jun 22, 2016 at 13:10
  • It does. 3v4l.org/tnCjA Commented Jun 22, 2016 at 13:14

2 Answers 2

3

Give a try to

return json_encode($data, JSON_PRETTY_PRINT);

This will give you pretty print of JSON format. Also what you have done should be working fine but what could be wrong is you are missing the JSON header.

header('Content-Type: application/json');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, header('Content-Type: application/json'); work for me without JSON_PRETTY_PRINT flag.
Great, try to use the json_preety_print as well to clean you json data and make it readable
-1
<?php
echo json_encode(
  array(
    'error' => array(
      'error_message' => 'Please choose a date and time',
      'error_code' => 500,
    )
  )
);
?>

Comments

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.