简体   繁体   English

json_encode()的问题

[英]Problem with json_encode()

i have an simple array: 我有一个简单的数组:

array
  0 => string 'Kum' (length=3)
  1 => string 'Kumpel' (length=6)

when I encode the array using json_encode(), i get following: 当我使用json_encode()编码数组时,我得到以下信息:

["Kum","Kumpel"] 

My question is, what is the reason to get ["Kum","Kumpel"] instead of { "0" : "Kum", "1" : "Kumpel" } ? 我的问题是,得到["Kum","Kumpel"]而不是{ "0" : "Kum", "1" : "Kumpel" }什么?

"{}" brackets specify an object and "[]" are used for arrays according to JSON specification. “{}”括号指定一个对象,“[]”用于根据JSON规范的数组。 Arrays don't have enumeration, if you look at it from memory allocation perspective. 如果从内存分配角度来看,数组没有枚举。 It's just data followed by more data, objects from other hand have properties with names and the data is assigned to the properties, therefore to encode such object you must also pass the correct property names. 它只是数据后跟更多数据,来自其他手的对象具有带名称的属性,并且数据被分配给属性,因此要编码此类对象,您还必须传递正确的属性名称。 But for array you don't need to specify the indexes, because they always will be 0..n, where n is the length of the array - 1, the only thing that matters is the order of data. 但对于数组,您不需要指定索引,因为它们总是为0..n,其中n是数组的长度 - 1,唯一重要的是数据的顺序。

$array = array("a","b","c");
json_encode($array); // ["a","b","c"]
json_encode($array, JSON_FORCE_OBJECT); // {"0":"a", "1":"b","2":"c"}

The reason why JSON_FORCE_OBJECT foces it to use "0,1,2" is because to assign data to obeject you must assign it to a property, since no property names are given by developer (only the data) the encoder uses array indexes as property names, because those are the only names which would make sense. JSON_FORCE_OBJECT将其用于“0,1,2”的原因是因为要将数据分配给obeject,必须将其分配给属性,因为开发人员没有给出属性名称(只有数据)编码器使用数组索引作为属性名字,因为那些是唯一有意义的名字。

Note: according to PHP manual the options parameters are only available from PHP 5.3. 注意:根据PHP手册 ,选项参数仅适用于PHP 5.3。

For older PHP versions refer to chelmertz's answer for a way to make json_encode to use indexes. 对于较旧的PHP版本,请参阅chelmertz的答案,了解如何使json_encode使用索引。

As Gumbo said, on the JS-side it won't matter. 正如Gumbo所说,在JS方面它并不重要。 To force PHP into it, try this: 要强制PHP进入它,请尝试以下方法:

$a = new stdClass();
$a->{0} = "Kum";
$a->{1} = "Kumpel";
echo json_encode($a);

Not that usable, I'd stick with the array notation. 不是那么可用,我坚持使用数组表示法。

Just cast as an object and it will work fine...the JSON_FORCE_OBJECT parameter does exactly the same thing. 只是作为一个对象进行转换,它会正常工作...... JSON_FORCE_OBJECT参数完全相同。

json_encode((object)$array);

Don't forget to convert it back into a php array so you can access its values in php: 不要忘记将其转换回php数组,以便您可以在php中访问它的值:

$array = (object)$array;
$array = (array)$array;

json_encode($array);

Since you're having a PHP array with just numeric keys, there is no need to use a JavaScript object. 由于您的PHP数组只有数字键,因此无需使用JavaScript对象。 But if you need one, try Maiku Mori's suggestion. 但如果您需要,请尝试Maiku Mori的建议。

I personally think this is a bug that needs to be fixed in PHP. 我个人认为这是一个需要在PHP中修复的错误。 JSON_FORCE_OBJECT is absolutely not an answer. JSON_FORCE_OBJECT绝对不是答案。 If you try to do any sort of generic programming you get tripped up constantly. 如果您尝试进行任何类型的通用编程,您将不断被绊倒。 For example, the following is valid PHP: 例如,以下是有效的PHP:

array("0" => array(0,1,2,3), "1" => array(4,5,6,7)); array(“0”=> array(0,1,2,3),“1”=> array(4,5,6,7));

And should be converted to 并应转换为

{"0": [0,1,2,3], "1": [4,5,6,7]} {“0”:[0,1,2,3],“1”:[4,5,6,7]}

Yet PHP expects me to either accept 但PHP希望我接受

[[0,1,2,3],[4,5,6,7]] [[0,1,2,3],[4,5,6,7]]

or 要么

{"0":{"0":1,"1":1,"2":2,"3":3},"1":{"0":4,"1":5,"2":6,"3":7}} { “0”:{ “0”:1, “1”:1, “2”:2 “3”:3}, “1”:{ “0”:4 “1”:5“,2 “:6,” 3" :7}}

Neither of which are right at all. 两者都不对。 How can I possibly decode an object like that? 我怎么可能解码这样的对象? What possible reason is there to ever change something that is clearly using strings as indexes? 有什么可能的原因可以改变一些明显使用字符串作为索引的东西? It's like PHP was trying to be clever to help out idiotic people who can't differentiate strings from ints, but in the process messed up anyone legitimately using strings as indexes, regardless of what the value COULD be turned into. 这就像PHP试图聪明地帮助那些无法区分字符串和贪婪的愚蠢人一样,但是在这个过程中,任何合法地使用字符串作为索引的人都会搞砸,无论可能变成什么样的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM