js解析处理JSON格式的数据,有多种方法.
(1)现在大多标准浏览器,都带有对JSON的支持,可以直接通过js调用处理:
var?obj=JSON.parse(str);//str是php传来的json数据
var?obj=jQuery.parseJSON(str);//str是php传来的json数据
var?obj?=?eval?("("?◆?txt?◆?")");//txt是php传来的json数据
但是使用eval不安全,而且需要是有效的json数据.如果你能保证是安全且正确的数据,可以这样简单处理.
给出例子供参考:
php
// Encode the data.
$json = json_encode(
array(
①. = array(
'English' = array(
'One',
'January'
),
'French' = array(
'Une',
'Janvier'
)
);
// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name = $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
// Show the errors for different depths.
var_dump(json_decode($json, true, $depth));
echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
json_encode
对中文的处理是有问题的:
不能处理GBK编码,所有的GBK编码都会替换成空字符;
处理的结果.
json_encode的返回结果稍加处理就可以得到正确的结果了.
json_decode之后需要显示或使用时,再转回来
用json_decode函数将json字符串转换为数组
echo "pre";
print_r(json_decode($json, true));
echo "/pre";
你 定义$str ="你那一大堆字符串".
然后 用 $arr=json_decode($str);
返回数组,
还有你的格式好像json_decode不出来哦,把格式整理一下,不行就用正则表达式吧
以上就是土嘎嘎小编为大家整理的在php中解析转义的json相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!