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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.