Jump to content

[SOLVED] php json decode


jzdexta

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/140657-solved-php-json-decode/
Share on other sites

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>

<?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);

?>

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())

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'])));

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??

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.