这种类型的字符串如何变成对象,json_decode结果为null,content的内容看做字符串

徐卡丘 2018-08-22 07:15:26
[{"id":"288","name":"test",,"identity":"TES","content":"{"module":"TEST","label":"ceshi","description":"this is a test"}","status":"0"}]
...全文
291 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2018-08-23
  • 打赏
  • 举报
回复
开发中可能用到的算法,php 基本上都给你准备好了,你只需要发挥的的想象就可以了
$a = Array(
'id' => 288,
'name' => 'test',
'desc' => 'test',
'identity' => 'TEST'
);
$b = Array(
'name' => 'test2',
'desc' => 'modify test',
'schemaTemplateId' => 1,
'sortNo' => 1
);

print_r(array_intersect_key($a, $b));
print_r(array_intersect_key($b, $a));

Array
(
[name] => test
[desc] => test
)
Array
(
[name] => test2
[desc] => modify test
)
徐卡丘 2018-08-23
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
有没有问题,请自行检验
正常情况下,程序是不会产生你示例的字符串的,所以并不会出意外
$a = [ 'a'=>1, 'b' => json_encode(['a'=>'a'])];
echo json_encode($a);
//{"a":1,"b":"{\"a\":\"a\"}"}
但你非要手工胡来的话,纰漏总是堵不完的

嗯嗯,那个,我现在有两个数组,返回两个具有相同键名部分的数组:
Array
(
[id] => 288
[name] => test
[desc] => test
[identity] => TEST
)
Array
(
[name] => test2
[desc] => modify test
[schemaTemplateId] => 1
[sortNo] => 1
)
返回
Array
(
[name] => test
[desc] => test
)
Array
(
[name] => test2
[desc] => modify test
)
xuzuning 2018-08-23
  • 打赏
  • 举报
回复
有没有问题,请自行检验
正常情况下,程序是不会产生你示例的字符串的,所以并不会出意外
$a = [ 'a'=>1, 'b' => json_encode(['a'=>'a'])];
echo json_encode($a);
//{"a":1,"b":"{\"a\":\"a\"}"}
但你非要手工胡来的话,纰漏总是堵不完的
徐卡丘 2018-08-23
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
$s =<<< TXT
[{"id":"288","name":"test","identity":"TES","content":"{"module":"TEST","label":"ceshi","description":"this is a test"}","status":"0"}]
TXT;
$s = preg_replace_callback('/("{)(.+)(}")/', function($m) { return $m[1] . addslashes($m[2]) . $m[3];}, $s);
print_r(json_decode($s));

刚刚百度到还有个addcslashes函数,这样我就可以只转义“,如果我用addcslashes,不知道其他字符会不会影响json_decode的结果??
xuzuning 2018-08-23
  • 打赏
  • 举报
回复
$s =<<< TXT
[{"id":"288","name":"test","identity":"TES","content":"{"module":"TEST","label":"ceshi","description":"this is a test"}","status":"0"}]
TXT;
$s = preg_replace_callback('/("{)(.+)(}")/', function($m) { return $m[1] . addslashes($m[2]) . $m[3];}, $s);
print_r(json_decode($s));
徐卡丘 2018-08-23
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
$s =<<< TXT
[{"id":"288","name":"test","identity":"TES","content":"{\"module\":\"TEST\",\"label\":\"ceshi\",\"description\":\"this is a test\"}","status":"0"}]

TXT;
print_r(json_decode($s));

Array
(
[0] => stdClass Object
(
[id] => 288
[name] => test
[identity] => TES
[content] => {"module":"TEST","label":"ceshi","description":"this is a test"}
[status] => 0
)

)

{"module":"TEST","label":"ceshi","description":"this is a test"}有没有什么函数可以把这个里面的分号转义一下?
xuzuning 2018-08-23
  • 打赏
  • 举报
回复
那你就不看手册的吗?
徐卡丘 2018-08-23
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
开发中可能用到的算法,php 基本上都给你准备好了,你只需要发挥的的想象就可以了
$a = Array(
'id' => 288,
'name' => 'test',
'desc' => 'test',
'identity' => 'TEST'
);
$b = Array(
'name' => 'test2',
'desc' => 'modify test',
'schemaTemplateId' => 1,
'sortNo' => 1
);

print_r(array_intersect_key($a, $b));
print_r(array_intersect_key($b, $a));

Array
(
[name] => test
[desc] => test
)
Array
(
[name] => test2
[desc] => modify test
)

谢谢版主,我感觉这种函数会有,就是可能用得少,我也没百度到
xuzuning 2018-08-22
  • 打赏
  • 举报
回复
$s =<<< TXT
[{"id":"288","name":"test","identity":"TES","content":"{\"module\":\"TEST\",\"label\":\"ceshi\",\"description\":\"this is a test\"}","status":"0"}]

TXT;
print_r(json_decode($s));

Array
(
[0] => stdClass Object
(
[id] => 288
[name] => test
[identity] => TES
[content] => {"module":"TEST","label":"ceshi","description":"this is a test"}
[status] => 0
)

)

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧