如何在PHP中解析JSON对象? [英] How do I parse a JSON object in PHP?

查看:117
本文介绍了如何在PHP中解析JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要发送到PHP脚本的JSON对象,无法解析JSON.这是POST请求:

I've got a JSON object that I'm sending to a PHP script and I'm having trouble parsing the JSON. Here's the POST request:

http://mywebsite.com?action=somefunction&{%22id%22:1,%22Name%22:%22Mike%22}

这是我的PHP函数,显然不起作用:

And here's my PHP function, which obviously doesn't work:

$data = $_GET['data'];
$obj = json_decode($data);
echo $obj->Name;
die();

最终目标是从URL字符串中提取名称"Mike".有什么建议吗?

The end goal is to extract the name "Mike" from the URL string. Any suggestions?

推荐答案

尝试查看PHP从json_decode()输出的内容:

Try taking a look at what PHP is outputting from json_decode():

$data = $_GET['data'];
$obj = json_decode($data);
var_dump($obj);

您的代码本身可以正常工作: http://ideone.com/0jsjgT

Your code itself works fine: http://ideone.com/0jsjgT

但是您的查询字符串在实际JSON之前缺少data=.这个:

But your query string is missing the data= before the actual JSON. This:

http://mywebsite.com?action=somefunction&{%22id%22:1,%22Name%22:%22Mike%22}

应该是这样:

http://mywebsite.com?action=somefunction&data={%22id%22:1,%22Name%22:%22Mike%22}

这篇关于如何在PHP中解析JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆