csplrj Posted January 20, 2010 Share Posted January 20, 2010 Below is the code I have written for php json decoding. Can't understand the reason Thanks in advance CSJakharia PHP File <? $pages=$_POST['pages']; if($pages!=null) { $pages1=json_decode($pages); //echo((json_encode(array('a'=>1,'b'=>2)))); echo($pages); var_dump($pages1); echo PHP_EOL; //echo($pages1); echo(' JSONERROR='.json_last_error()); } ?> <form method="post" id="addPage"> <input type="hidden" value='[{"id":0},{"id":64, "websiteId":14, "pageName":"Home", "parentPageId":0, "pageTypeId":1, "orderNo":1, "pages":[]},{"id":65, "websiteId":14, "pageName":"a", "parentPageId":0, "pageTypeId":1, "orderNo":2, "pages":[{"id":66, "websiteId":14, "pageName":"b", "parentPageId":65, "pageTypeId":1, "orderNo":3, "pages":[]]},{"id":66, "websiteId":14, "pageName":"b", "parentPageId":65, "pageTypeId":1, "orderNo":3, "pages":[]},{"id":67, "websiteId":14, "pageName":"c", "parentPageId":0, "pageTypeId":1, "orderNo":4, "pages":[]}]' id="pages" name="pages"/> <input type="hidden" value='[{"id":0},{"id":64, "websiteId":14, "pageName":"Home", "parentPageId":0, "pageTypeId":1, "orderNo":1, "pages":[]}]' id="pages" name="pages1"/> <input type=submit> </form> But this output comes NULL JSONERROR=2 Quote Link to comment https://forums.phpfreaks.com/topic/189119-php-json_decode-not-decoding/ Share on other sites More sharing options...
trq Posted January 20, 2010 Share Posted January 20, 2010 Can you echo $pages to see what it looks like? Quote Link to comment https://forums.phpfreaks.com/topic/189119-php-json_decode-not-decoding/#findComment-998446 Share on other sites More sharing options...
csplrj Posted January 21, 2010 Author Share Posted January 21, 2010 Below is the output for $pages [{"id":0},{"id":64, "websiteId":14, "pageName":"Home", "parentPageId":0, "pageTypeId":1, "orderNo":1, "pages":[]},{"id":65, "websiteId":14, "pageName":"a", "parentPageId":0, "pageTypeId":1, "orderNo":2, "pages":[{"id":66, "websiteId":14, "pageName":"b", "parentPageId":65, "pageTypeId":1, "orderNo":3, "pages":[]]},{"id":66, "websiteId":14, "pageName":"b", "parentPageId":65, "pageTypeId":1, "orderNo":3, "pages":[]},{"id":67, "websiteId":14, "pageName":"c", "parentPageId":0, "pageTypeId":1, "orderNo":4, "pages":[]}] Quote Link to comment https://forums.phpfreaks.com/topic/189119-php-json_decode-not-decoding/#findComment-999129 Share on other sites More sharing options...
salathe Posted January 21, 2010 Share Posted January 21, 2010 How are you creating the JSON values because they're malformed: for the last one you posted there was an extra ] and some missing ]} chars. A helpful friend here is http://www.jsonlint.com Quote Link to comment https://forums.phpfreaks.com/topic/189119-php-json_decode-not-decoding/#findComment-999277 Share on other sites More sharing options...
csplrj Posted January 22, 2010 Author Share Posted January 22, 2010 Thanks for the link given as it helped me in identifying the issue I changed the json string and then When I try it does not works in PHP Version 5.2.4 but it works on PHP Version 5.3.1. I have my shared hosting on PHP Version 5.2.4. How to do the same <? $pages=$_POST['pages']; if($pages!=null) { $pages1=json_decode($pages); var_dump($pages1); } ?> <form method="post" id="addPage"> <input type="hidden" value='[{"parentPageId":0, "id":0},{"parentPageId":0, "id":68},{"parentPageId":0, "id":69},{"parentPageId":69, "id":70},{"parentPageId":0, "id":71}]' id="pages" name="pages"/> <input type=submit> </form> Thanks in advance CSJakharia Quote Link to comment https://forums.phpfreaks.com/topic/189119-php-json_decode-not-decoding/#findComment-999698 Share on other sites More sharing options...
csplrj Posted January 22, 2010 Author Share Posted January 22, 2010 I got the issue was I have to use instead of $pages1=json_decode($pages); I have to write $pages1=json_decode(stripslashes($pages)); in the Shared hosting Thanks in advance CSJakharia Quote Link to comment https://forums.phpfreaks.com/topic/189119-php-json_decode-not-decoding/#findComment-999717 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.