如何将包含名称 - 值对的String转换为关联数组或其他支持名称 - 值对的结构?

时间:2016-05-11 17:19:11

标签: php json associative-array stdclass

我有一个看起来像这样的字符串:

{"red":216.77,"blue":0.669,"green":0.545,"alpha":1}

我想将它保存在这样的数组中:

array(
  "red" => 216.77,
  "blue" => 0.669,
  green" => 0.545,
  "alpha" => 1
)

或包含名称 - 值对的其他结构(如stdclass),以便我可以遍历它并按名称检索值。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

$object = json_decode("JSON response goes here")

$object->property