【问题标题】:json_encode() automatically converts array into objectjson_encode() 自动将数组转换为对象
【发布时间】:2014-03-11 19:09:10
【问题描述】:

当我在PHP 中使用json_encode() 对数组进行编码时,一切都已完成,但当我使用json_decode() 对其进行解码时,它给了我stdClass 对象而不是数组。 这是我的代码:

echo $json=json_encode(array('a'=>1,'b'=>2));
echo '<br>';
print_r(json_decode($json));

// and the result of PHP script is :
{"a":1,"b":2}
// stdClass Object ( [a] => 1 [b] => 2 )

是什么将它从数组转换为对象??

我可以将 json 编码的字符串传递到 url 以获取另一个页面上的数据,或者是否存在任何可以进行一些 url 编码的函数?

【问题讨论】:

标签: php json


【解决方案1】:

json_decode() 需要一个额外的参数来解码为数组。

json_decode($json, true)

【讨论】:

  • 我可以将 json 编码的字符串传递给 url 以获取另一个页面上的数据,或者是否存在任何可以进行 url 编码的函数?
  • 你想一起使用它,它就像urlencode((json_decode($json, true))一样工作。
  • 但是,什么是对象的多维数组被传递?它递归地将对象转换为数组。
猜你喜欢
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 2021-07-07
  • 1970-01-01
  • 2021-05-03
  • 2018-12-04
  • 2021-06-16
相关资源
最近更新 更多