0

Hey guys,

I am having some trouble with json_decode...
I used json_encode to encode a string that is the following:

{"id":"121","firm":"DSD","address":"Test","address2":"Test","city":"Test","region":"Test","postcode":"Test","countryid":"13","firmtype":"Marketing","phone":"01545454587","fax":"","email":"dan@test.com","url":"http://www.test.com","mainuser":"Dan","contacts":"dan@test.com","info":"","numstaff":"15","numoffices":"1","disc":"","cat":"{"35":"1"}"}

And I am using this php code to try and get it back into an array... however its just not working... the print_r(); doesnt echo anything out at all... help pleaseee!!

PHP:

if(isset($_GET['id'])){
    $arraydata = mysql_fetch_assoc(mysql_query("SELECT * FROM `changelog` WHERE `id` = '".$_GET['id']."'"));
    $newdata = json_decode($arraydata['newda'], true);
    $olddata = json_decode($arraydata['orig'], true);
    print_r($newdata);
}

Dan

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by Danny159
0

Use var_dump instead. This will allow you to determine whether json_decode is returning null because the JSON cannot be decoded.

If you're pulling the data from a database, did you add slashes or anything when inserting?

0

Thanks for your reply, I added var_dunp and it returned NULL, I dont addslashes when inserting all I do it jason_encode: see below...

$thedarray = json_encode($thedarray);
    mysql_query("INSERT INTO `changelog` (`date`,`newda`,`orig`,`status`) VALUES ('".time()."','$thedarray','$orgarray','1')");
0

Ahhh I have a json in a json will that cause issues, if so how would you resolve it?

0

Why would you need a JSON string inside another JSON string?

Encode the entire data structure in one go.

0
var_dump($arraydata['newda']);

How look like the above value before json_decode() function ? It contains any slash or escape ?

0

Its alright guys I sorted it... your right I needed to addslashes :)

Thanks!
Dan

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.