PHP json_encode问题与数组0键。

时间:2021-02-20 21:34:44

I am having a problem using json_encode to generate a json encoded string from an array.

我使用json_encode从数组生成json编码的字符串有问题。

The section of the array in question looks like this

该数组的部分如下所示

RatingDistribution (Array, 11 elements)
    0 (Array, 1 element)
        0 (String, 3 characters ) 4.5
    1 (Array, 1 element)
        1 (String, 4 characters ) 11.9
    2 (Array, 1 element)

But produces this in the string:

但是在字符串中产生了这个

"RatingDistribution":[["4.5"],{"1":"11.9"},

I would expect this:

我希望这个:

"RatingDistribution":[{"0":"4.5"},{"1":"11.9"},

All I'm doing is this:

我所做的就是:

$result = json_encode($array);

Have I done something wrong or do I need more code to ensure the 0 key is present?

我做错什么了吗?还是我需要更多的代码来确保0键存在?

Cheers Andy

欢呼声安迪

3 个解决方案

#1


14  

The result you are getting should be expected; json_encode detects that you are only using numeric keys in the array, so it translates that to an array instead of an object in JSON. Most of the time, that's exactly what you want to do.

你得到的结果应该是预期的;json_encode检测到您只在数组中使用数字键,因此它将其转换为数组,而不是JSON中的对象。大多数时候,这正是你想做的。

If for some reason you don't (why?), in PHP >= 5.3 you can use the JSON_FORCE_OBJECT flag to get your desired output:

如果由于某种原因您不知道(为什么?),在PHP >= 5.3中,您可以使用JSON_FORCE_OBJECT标志来获得所需的输出:

$result = json_encode($array, JSON_FORCE_OBJECT);

#2


1  

If you want to use arrays in your json then instead of JSON_FORCE_OBJECT parameter you can simply cast array to object.

如果希望在json中使用数组,那么只需将数组转换为对象,而不是JSON_FORCE_OBJECT参数。

Problem:

问题:

json_encode([0 => [1,2,3]]); // Return: [[1,2,3]]
json_encode(["0" => [1,2,3]]); // Return: [[1,2,3]]
json_encode([1 => [1,2,3]]); // Return: {"1":[1,2,3]}

Not what we expect :

不是我们期望的:

json_encode([0 => [1,2,3]], JSON_FORCE_OBJECT); // Return: {"0":{"0":1,"1":2,"2":3}}

Solution:

解决方案:

json_encode((object)[0 => [1,2,3]]); // Return: {"0":[1,2,3]}
json_encode(new \ArrayObject([0 => [1,2,3]])); // Return: {"0":[1,2,3]}

#3


-1  

Cou can try to cast the array key to a string for example with strval or (string).

Cou可以尝试将数组键强制转换为字符串,例如使用strval或(string)。

#1


14  

The result you are getting should be expected; json_encode detects that you are only using numeric keys in the array, so it translates that to an array instead of an object in JSON. Most of the time, that's exactly what you want to do.

你得到的结果应该是预期的;json_encode检测到您只在数组中使用数字键,因此它将其转换为数组,而不是JSON中的对象。大多数时候,这正是你想做的。

If for some reason you don't (why?), in PHP >= 5.3 you can use the JSON_FORCE_OBJECT flag to get your desired output:

如果由于某种原因您不知道(为什么?),在PHP >= 5.3中,您可以使用JSON_FORCE_OBJECT标志来获得所需的输出:

$result = json_encode($array, JSON_FORCE_OBJECT);

#2


1  

If you want to use arrays in your json then instead of JSON_FORCE_OBJECT parameter you can simply cast array to object.

如果希望在json中使用数组,那么只需将数组转换为对象,而不是JSON_FORCE_OBJECT参数。

Problem:

问题:

json_encode([0 => [1,2,3]]); // Return: [[1,2,3]]
json_encode(["0" => [1,2,3]]); // Return: [[1,2,3]]
json_encode([1 => [1,2,3]]); // Return: {"1":[1,2,3]}

Not what we expect :

不是我们期望的:

json_encode([0 => [1,2,3]], JSON_FORCE_OBJECT); // Return: {"0":{"0":1,"1":2,"2":3}}

Solution:

解决方案:

json_encode((object)[0 => [1,2,3]]); // Return: {"0":[1,2,3]}
json_encode(new \ArrayObject([0 => [1,2,3]])); // Return: {"0":[1,2,3]}

#3


-1  

Cou can try to cast the array key to a string for example with strval or (string).

Cou可以尝试将数组键强制转换为字符串,例如使用strval或(string)。