将JSON转换成数组形式的代码共享
一个JSON被转换成一个数组和一个方法。事实上,编写方法可以将包含JSON字符串的大多数数据结构转换成数组。复制代码代码如下所示:
类antitranjson
{
保护静态函数jsontoarray($ JSON)
{
如果(!is_string($ JSON)| | is_null(json_decode($ JSON,真)))
把新的notjsonstringexception('param不是JSON字符串);
dejson = json_decode美元($ JSON,真的);
回归自我::toArray($ dejson);
}
保护静态函数stdclasstoarray($性病)
{
如果(is_object($性病))
把新的notobjectexception('params没有对象);
get_object_vars $params =(美元性病);
回归自我::toArray($params);
}
保护静态函数arrayrtoarray($params)
{
$ =数组();
如果(!is_array($params))
把新的notarrayexception('params不是数组);
foreach($params为K = > $ V)
{
$ TMP { $ k} =自我::toArray($ V);
}
/ / var_dump($ TMP);
返回的TMP;
}
调用这个方法,包括JSON数据可以转换。
公共静态函数toArray($params)
{
$ =数组();
如果(is_string($params)!is_null(json_decode($params)))
$ TMP =自我::jsontoarray($params);
(is_array($params))
$ TMP =自我::arrayrtoarray($params);
/ / here note that if $params is an object that only contains the attribute is readable (public or temporary object properties can be realized when the conversion)
(is_object($params))
$ TMP =自我::stdclasstoarray($params);
其他的
$ TMP = $params;
返回的TMP;
}
以上是相关代码,至少目前可以使用,如果你有好的建议,希望大家讨论讨论,进展,谢谢。