开发者

Why can't I get PHP to show me a single json element

Man this JSON thing开发者_运维知识库 is chewing away at my day. Is it suppose to be this difficult? Probably not. Ok, so I am receiving a URL with a json data set in it.

It looks like this:

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}

And when I run json_decode in PHP on it, it looks like this:

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) "" } 

I guess I should mention it was encoded as a serialized object from the form page and then encoded and sent over...Don't know if that will make a difference.

Anyway, I dutifully check the PHP manual, and everything, as always, looks simple enough to implement. And then, of course, I try it just the way they tell me and I miss something that's probably obvious to everyone here but me. This bit of code, returns nothing except my text that I'm echoing:

<?php
$json = $_GET['jsonval'];
$obj = var_dump(json_decode($json));

echo "<br><br>ELEMENT PLEASE!" . $obj;
print $obj->{"Fname"}; // 12345

?>

I mean, all I want is to see the values of my individual key/values and print them out. What have I done wrong here?

Thanks for any advice.


This line is completely wrong:

$obj = var_dump(json_decode($json));

var_dump() returns nothing

You need:

$obj = json_decode($json);

Turn display_errors on in your php.ini and set up ERROR_REPORTING = E_ALL. And continue developing with such settings.


You put this: print $obj->{"Fname"}; // 12345

It should be print $obj->Fname; // 12345


I think you are not calling out data from the object incorrectly.

Needs to be something like:

$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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜