如何确保PHP生成的JSON中的所有键名都是字符串? [英] How can I ensure all key names in a PHP generated JSON are strings?

查看:74
本文介绍了如何确保PHP生成的JSON中的所有键名都是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,可输出JSON数据,该数据由浏览器中的Javascript以及iPhone和Android应用程序进行解析.

I have a PHP script that outputs JSON data which is parsed by Javascript in Browsers, and also by iPhone and Android apps.

以前,我发现PHP将数组数据转换为JSON时,会将所有数字值转换为字符串,如双引号所示.因此,例如,"id" : 1将变为"id" : "1".这似乎使接收到JSON数据的某些Javascript函数感到困惑,因此我在PHP中的JSON上运行了此正则表达式,以确保未将数字制成字符串:

Previously, I found that when PHP converted array data into a JSON, it would make all numberical values into strings as indicated by double quotes. So, for example, "id" : 1 would become "id" : "1". This seemed to confuse some of the Javascript functions that received the JSON data, so I ran this regular expression on the JSON in PHP in order to make sure numbers weren't made into strings:

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

但是,这也具有删除键和值中的双引号的作用,因此1 : "first value"变为"1" : "first value".事实证明这是一个问题,因为如果JSON密钥不是字符串,那么接收数据的iPhone应用程序就会抱怨.

However, this also has the effect of removing double quotes from both keys and values, so 1 : "first value" becomes "1" : "first value". It turns out this is a problem because if JSON keys aren't strings, then the iPhone app that recieves the data complains.

所以,我要做的是确保JSON中的所有值都保留为整数,但所有键都制成字符串.有没有一种方法可以仅对值运行上面的正则表达式?还是其他任何可以给我类似结果的过程?

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

推荐答案

有没有一种方法可以仅对值运行上面的正则表达式?

Is there a way I can run the regular expression I have above on just the values?

您最好 避免对字符串进行后处理.

You're much better off avoiding post-processing the string.

以前,我发现PHP将数组数据转换为JSON时,会将所有数字值转换为字符串,如双引号所示.因此,例如,"id":1将变为"id":"1".

Previously, I found that when PHP converted array data into a JSON, it would make all numberical values into strings as indicated by double quotes. So, for example, "id" : 1 would become "id" : "1".

这里的答案是确保数字值确实是数字.如果您在期望数字的地方看到带有数字的字符串,那是因为PHP对象/数组中的值不是数字.这就是您需要解决的问题.

The answer here is to make sure that the number values really are numbers. If you're seeing strings with digits in them where you expect numbers, it's because the value in the PHP object/array is not a number. That's what you need to fix.

例如,此代码:

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

...相当正确地产生以下输出:

...quite correctly produces this output:

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

请注意,在PHP代码中,实际上是数字的值是如何在JSON中以数字形式出现的.

Note how the value that's really a number in the PHP code comes out as a number in the JSON.

因此,不是对字符串进行后处理,而是要更正要输入到json_encode的数据.

So rather than post-processing the string, the answer is to correct the data you're feeding into json_encode.

在下面回复您的评论:

我知道我可以为键专门指定字符串名称,但是有问题的数组是通过while循环自动生成的,该循环保留附加的数字,并且这些数字用于计数操作,因此我认为这不起作用就我而言.

I understand that I could specifically assign string names to keys, but the array in question is generated automatically with a while loop that keeps appending numbers, and those numbers are used in counting operations, so I don't think this would work in my case.

没有任何区别.例如,这段代码:

It doesn't make the slightest difference. 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);
?>

产生以下输出:

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

再次注意,数字在输入中是数字,因此在输出中是数字.

Again note that the numbers are numbers in the input, and therefore numbers in the output.

还请注意,如果对象的是数字,则PHP会正确处理它:

Also note that PHP correctly handles 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);
?>

产生

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

这篇关于如何确保PHP生成的JSON中的所有键名都是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆