3

How can I let my PHP server respond with some thing like this:

{"eCode":0,"city":"london","time":"15:35"}

not:

{"eCode":"0","city":"london","time":"15:35"}

I'm using this PHP code:

<?php
    $data = array('eCode' => '0', 'city' => 'london', 'time' => '15:35');
    header('Content-type: application/json');
    echo json_encode($data);
?>

So how can I remove the quotes from "eCode":"0", and replace it with "eCode":0, ?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
dony82
  • 31
  • 2

2 Answers2

1

You're quoting the zero, making it a string. Just leave it as a int:

<?php
$data = array('eCode' => 0, 'city' => 'london', 'time' => '15:35');
echo json_encode($data);
?>
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • 1
    @Xorifelse: With all due respect, the comments are not for answers. If u_mulder wanted to answer, there is a way to post an answer. – AbraCadaver Nov 21 '16 at 21:07
  • @AbraCadaver Just to clarify, I did not down vote. All I was saying when this answer was posted the OP already made the statement u_mulders solution worked. Its not that hard to add that to an answer. – Xorifelse Nov 21 '16 at 21:21
1

This answer reiterates some things that were already said by others, but hopefully a little more explanation will be helpful.

Basically, the zero is quoted in the output of json_encode($data) because it is quoted in the input.

json_encode will not automatically convert numeric strings to integers, unless you have set the JSON_NUMERIC_CHECK option, for example:

echo json_encode($data, JSON_NUMERIC_CHECK);

If you are constructing the array you're attempting to json_encode with your code, then you should be able to simply add ints rather than numeric strings into the array as you build it:

$data['eCode'] = 0;         // use 0 instead of '0'
$data['city'] = 'london';
$data['time'] = '15:35';

However, if the array you're working with is coming from some other source, such as a query result, you'll need to decide between several options:

  1. Use JSON_NUMERIC_CHECK when you json_encode (keep in mind that it won't only convert that column to a number; it will convert anything that looks like a number - you can see various warnings about that in the comments on this answer and on the PHP documentation)

  2. Figure out how to make adjustments to the data source or the way you fetch the data so that the array you get will already have ints instead of strings (for example, turning off emulated prepares in PDO)

  3. Convert the value in the array before you json_encode. There are various ways to do that, $data['eCode'] = (int) $data['eCode'];, for example.

In my opinion, option 2 would be the best way to handle it.

Community
  • 1
  • 1
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Your answer is too focused on encoding, indirectly answering the question but still *very* informative. With this information, one can `decode -> encode (with JSON_NUMERIC_CHECK) -> decode` to get the desired result without changing the input. – Xorifelse Nov 21 '16 at 22:04
  • @Xorifelse The point I wanted to make (and maybe I need to edit to add more emphasis to that part) is that I think the right way to deal with this problem is to fix it as close to the source as possible, i.e. get an array with an int to begin with rather than trying to manipulate it after the fact. – Don't Panic Nov 21 '16 at 23:09