How can I guarantee that all the key names in PHP generated by JSON are strings?

I have a PHP script that outputs JSON data that is parsed by Javascript in browsers, as well as applications for iPhone and Android.

I used to find that when PHP converts array data to JSON, it turns all numeric values ​​into strings, as indicated by double quotes. So, for example, "id" : 1 will become "id" : "1" . This seemed to confuse some Javascript functions that received JSON data, so I ran this JSON regular expression in PHP to make sure the numbers were not written to strings:

 $JSONOutput = preg_replace('/"(-?\d+\.?\d*)"/', '$1', json_encode($JSONOutput)); 

However, this also removes the double quotes from both keys and values, so 1 : "first value" becomes "1" : "first value" . It turns out this is a problem, because if the JSON keys are not strings, then the iPhone application that receives the data complains.

So what I want to do is make sure all values ​​in JSON are stored as integers, but all keys are made into strings. Is there a way that I can run the regex that I have above, only for values? Or any other process that would give me a similar result?

+1
source share
1 answer

Is there a way to run the regex that I have above, only the values?

You are much better off avoiding post-processing the string.

I used to find that when PHP converts array data to JSON, it turns all numeric values ​​into strings, as indicated by double quotes. So, for example, "id": 1 will become "id": "1".

The answer here is to make sure that the numerical values ​​are indeed numbers. If you see strings with numbers in them where you expect a number, this is because the value in the PHP object / array is not a number. This is what you need to fix.

For example, this code:

 <?php header('Content-Type: application/json'); $x = array( "number" => 1, "string" => "2" ); echo json_encode($x); ?> 

... quite correctly produces this conclusion:

 {"number":1,"string":"2"} 

Notice how the value that is really a number in PHP code is output as a number in JSON.

Therefore, instead of post-processing the string, the response should correct the data that you load in json_encode .


Responding to your comment below:

I understand that I could specifically assign row names to the keys, but the array in question is generated automatically with a while loop that stores the number of additions, and these numbers are used in the counting operations, so I don't think this will work in my case.

It does not matter. For example, this code:

 <?php header('Content-Type: application/json'); $x = array(); for ($n = 1; $n < 5; ++$n) { $x["entry" . $n] = $n; } echo json_encode($x); ?> 

produces this conclusion:

 {"entry1":1,"entry2":2,"entry3":3,"entry4":4} 

Note again that numbers are numbers at the input, and therefore numbers at the output.

Also note that PHP correctly processes it if the keys of the object are numbers:

 <?php header('Content-Type: application/json'); $x = array(); $x["foo"] = "bar"; for ($n = 1; $n < 5; ++$n) { $x[$n] = $n * 2; } echo json_encode($x); ?> 

produces

 {"foo":"bar","1":2,"2":4,"3":6,"4":8} 
+2
source

All Articles