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

Recommended Answers

All 6 Replies

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?

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

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

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

Encode the entire data structure in one go.

var_dump($arraydata['newda']);

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

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

Thanks!
Dan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.