-3

我需要从给定的字符串创建一个数组或一个对象。字符串在帖子中接收,是一个序列化的 js 对象。

我得到一个像这样的字符串:

{'id': n, 'propery 1': 'something', 'propery 2': 'something else', etc.} 

或者

{'whatever': bla bla, etc.} (without the id part). 

我需要将其转换为 php 对象或数组或......可用的东西。

我知道我可以提取子字符串,explode(': ', $string)但我效率不高。接收到的字符串非常接近 json 格式(只有 1 或 2 个例外,我可以单独处理)。

4

5 回答 5

3

使用 json_decode($string) ==> php 对象(或数组,如果你强制使用参数)。

使用 json_encode($array) ==> 字符串

于 2012-05-11T08:26:53.637 回答
1

看起来像 JSON。您可以使用 json_decode():http ://php.net/manual/en/function.json-decode.php

于 2012-05-11T08:25:57.820 回答
1

您可以在 PHP 中使用 JSON 解码方法:http: //php.net/manual/en/function.json-decode.php

于 2012-05-11T08:26:05.747 回答
1

这是您需要的 json_decode :http ://php.net/manual/en/function.json-decode.php

于 2012-05-11T08:26:21.370 回答
1

你可以使用json_encode(). http://in.php.net/manual/en/function.json-encode.php

于 2012-05-11T08:27:23.167 回答