这个JSON的东西在我的日子里咀嚼着。是否认为这很困难?可能不是。好的,所以我收到一个带有json数据集的URL。
看起来像这样:
jsonval={%22Fname%22:+%22kjhjhkjhk%22,+%22Lname%22:+%22ghghfhg%22,+%22conf[]%22:+[%22ConfB%22,+%22ConfA2%22],+%22quote%22:+%22meat%22,+%22education%22:+%22person%22,+%22edu%22:+%22welding%22,+%22Fname2%22:+%22%22,+%22Lname2%22:+%22%22,+%22gender%22:+%22B2%22,+%22quote2%22:+%22Enter+your+meal+preference%22,+%22education2%22:+%22person2%22,+%22edu2%22:+%22weld2%22,+%22jsonval%22:+%22%22}
当我在PHP上运行json_decode时,它看起来像这样:
object(stdClass)#1 (13) { ["Fname"]=> string(9) "kjhjhkjhk" ["Lname"]=> string(7) "ghghfhg" ["conf[]"]=> array(2) { [0]=> string(5) "ConfB" [1]=> string(6) "ConfA2" } ["quote"]=> string(4) "meat" ["education"]=> string(6) "person" ["edu"]=> string(7) "welding" ["Fname2"]=> string(0) "" ["Lname2"]=> string(0) "" ["gender"]=> string(2) "B2" ["quote2"]=> string(26) "Enter your meal preference" ["education2"]=> string(7) "person2" ["edu2"]=> string(5) "weld2" ["jsonval"]=> string(0) "" }
我想我应该提到它是从表单页面编码为序列化对象,然后编码并发送过来...不知道这是否会有所作为。
无论如何,我尽职地检查PHP手册,一切都像往常一样,看起来很简单,无法实现。然后,当然,我只是按照他们告诉我的方式来尝试,我想念一些对我这里的每个人都很明显的东西。这段代码,除了我正在回应的文本之外什么也不返回:
<?php
$json = $_GET['jsonval'];
$obj = var_dump(json_decode($json));
echo "<br><br>ELEMENT PLEASE!" . $obj;
print $obj->{"Fname"}; // 12345
?>
我的意思是,我想要的只是查看各个键/值的值并将其打印出来。我在这做错了什么?
感谢您的任何建议。
答案 0 :(得分:2)
这一行是完全错误的:
$obj = var_dump(json_decode($json));
var_dump()
不返回任何内容
你需要:
$obj = json_decode($json);
在php.ini中转动display_errors
on
并设置ERROR_REPORTING = E_ALL
。并继续开发这样的设置。
答案 1 :(得分:0)
你说这个:
print $obj->{"Fname"}; // 12345
应该是
print $obj->Fname; // 12345
答案 2 :(得分:0)
我认为你没有错误地从对象中调出数据。
需要像:
$data = (json_decode(urldecode('{%22Fname%22:+%22kjhjhkjhk%22,+%22Lname%22:+%22ghghfhg%22,+%22conf[]%22:+[%22ConfB%22,+%22ConfA2%22],+%22quote%22:+%22meat%22,+%22education%22:+%22person%22,+%22edu%22:+%22welding%22,+%22Fname2%22:+%22%22,+%22Lname2%22:+%22%22,+%22gender%22:+%22B2%22,+%22quote2%22:+%22Enter+your+meal+preference%22,+%22education2%22:+%22person2%22,+%22edu2%22:+%22weld2%22,+%22jsonval%22:+%22%22}')));
echo $data->Fname;