jzdexta Posted January 13, 2009 Share Posted January 13, 2009 Hi, Got this json data posted to a php form : [{"value": "12", "id": "24"}, {"value": "23", "id": "16"}, {"value": "34", "id": "11"}, {"value": "45", "id": "5"}, {"value": "56", "id": "4"}, {"value": "67", "id": "13"}, {"value": "78", "id": "2"}, {"value": "89", "id": "1"}, {"value": "90", "id": "nonAg"}] problem is when i decode this data i get a null value returned. Tried utf8-encoding before json_decode still getting null result. What could I be missing?? NB: Have validated the data on jsonlint dot com and it was valid. Regards -j Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 what version of PHP are you running? does this sample code work for you? <?php if($_SERVER['REQUEST_METHOD'] == 'POST'){ print '<pre style="border:solid 1px black;">'; $json = json_decode($_POST['json']); print_r($json); print '</pre>'; } ?> <form action="" method="POST"> <textarea name="json" style="width:500px;height:200px;"></textarea><br /> <input type="submit" /> </form> Quote Link to comment Share on other sites More sharing options...
jzdexta Posted January 13, 2009 Author Share Posted January 13, 2009 Im using php 5.2.5. When I use your code with any form of dummy text it shows what i've typed, however, if i copy & paste the result of my "faulty" json it just display a blank line. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 13, 2009 Share Posted January 13, 2009 Please post the code you're using. Ken Quote Link to comment Share on other sites More sharing options...
jzdexta Posted January 13, 2009 Author Share Posted January 13, 2009 <?php require_once('includes/mysql.lib.php'); $connObj = new connect(); $rxJson = $connObj->cleanVariable($_POST['consumption']);//mysql_real_escape_string -> htmlspecialchars -> stripslashes(var) //$rxJson = utf8_encode($rxJson); var_dump(json_decode($rxJson)); //$rxJson = json_decode($rxJson); //echo var_dump($rxJson); ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 don't alter the value of $_POST['consumption'] before json_decode() Quote Link to comment Share on other sites More sharing options...
jzdexta Posted January 13, 2009 Author Share Posted January 13, 2009 If i json_decode $_POST['consumption'] directly i get NULL The received value is [{\"value\": \"12\", \"id\": \"24\"}, {\"value\": \"23\", \"id\": \"16\"}, {\"value\": \"34\", \"id\": \"11\"}, {\"value\": \"45\", \"id\": \"5\"}, {\"value\": \"56\", \"id\": \"4\"}, {\"value\": \"67\", \"id\": \"13\"}, {\"value\": \"78\", \"id\": \"2\"}, {\"value\": \"89\", \"id\": \"1\"}, {\"value\": \"90\", \"id\": \"nonAg\"}] encoded with a javascript function (Prototype.js array#toJSON()) Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 ok...you have magic quotes turned on. i would recommend disabling magic quotes if you can as it is depreciated. here is info on that: http://us3.php.net/manual/en/security.magicquotes.disabling.php if you do disable it, you should remove the stripslashes() from your cleanVariable() method if you can't disable it, only use stripslashes() on the data before decoding: var_dump(json_decode(strip_slashes($_POST['consumption']))); Quote Link to comment Share on other sites More sharing options...
jzdexta Posted January 13, 2009 Author Share Posted January 13, 2009 Thanks at list now there's some constructive output This is the output i get array(9) { [0]=> object(stdClass)#2 (2) { ["value"]=> string(2) "12" ["id"]=> string(2) "24" } [1]=> object(stdClass)#3 (2) { ["value"]=> string(2) "23" ["id"]=> string(2) "16" } [2]=> object(stdClass)#4 (2) { ["value"]=> string(2) "34" ["id"]=> string(2) "11" } [3]=> object(stdClass)#5 (2) { ["value"]=> string(2) "45" ["id"]=> string(1) "5" } [4]=> object(stdClass)#6 (2) { ["value"]=> string(2) "56" ["id"]=> string(1) "4" } [5]=> object(stdClass)#7 (2) { ["value"]=> string(2) "67" ["id"]=> string(2) "13" } [6]=> object(stdClass)#8 (2) { ["value"]=> string(2) "78" ["id"]=> string(1) "2" } [7]=> object(stdClass)#9 (2) { ["value"]=> string(2) "89" ["id"]=> string(1) "1" } [8]=> object(stdClass)#10 (2) { ["value"]=> string(2) "90" ["id"]=> string(5) "nonAg" } } next question is, how do i get to access the individual object and there respective values?? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 $data = json_decode(strip_slashes($_POST['consumption'])); foreach($data as $item){ print $item->value; print $item->id; } Quote Link to comment Share on other sites More sharing options...
jzdexta Posted January 13, 2009 Author Share Posted January 13, 2009 Thanks a bunch!! Couldn't have made any step without your assistance Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.