xcandiottix Posted July 20, 2011 Share Posted July 20, 2011 I'm only working with PHP 5.2 so I don't have the option to include one of the bitmask arguments while encoding an array. I was wondering if someone had a work around for this: $_GLOBALS['History'] .= '['; while($row3 = @mysql_fetch_array($result3)){ $number = $row3['db_Id']; $time = $row3['db_Timestamp']; date_default_timezone_set('America/Phoenix'); $time = date("l M j g:i:s a T Y", $time); $_GLOBALS['History'] .= '"<img src=\'../images/avatar/'.md5($row['db_UserId']).'.jpg\'>'.$row['db_UserId'].'<br>'.$time.'<br>",'; unset($alert); } $_GLOBALS['History'] .= '"0"]'; //SETS LAST RUN AS SOMETHING TO FINALIZE LAST COMMA OF WHILE LOOP I get it to the JSON like so: $liveArray['Stream'] = $_GLOBALS['History']; $data = json_encode($liveArray); $myFile = '../json/auctions/'.$ID.'-live.js'; $fh = fopen($myFile, 'w') or die('can\'t open file'); fwrite($fh, $data); fclose($fh); //Set file access chmod('../json/auctions/'.$ID.'-live.js', 0644); //TESTING echo $data; unset($liveArray, $data, $myFile, $fh); In my JSON file I'll see: {"Stream":"[\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"<img src='..\/images\/avatar\/d41d8cd98f00b204e9800998ecf8427e.jpg'><br>Wednesday Dec 31 5:00:00 pm MST 1969<br><br>\",\"0\"]"} But the proper format of a json array should be {"Stream":["item one","item two","etc"]"} instead of {"Stream":"["item one","item two","etc"]""} So how can I avoid that " after the : ? Quote Link to comment https://forums.phpfreaks.com/topic/242404-php-to-json-issue/ Share on other sites More sharing options...
dcro2 Posted July 20, 2011 Share Posted July 20, 2011 You should post more code. Instead of building your own json array, why not just let the function take care of it? Make Stream an array and json_encode will make it a proper json array. Otherwise, why not just build the whole json string by yourself?? Quote Link to comment https://forums.phpfreaks.com/topic/242404-php-to-json-issue/#findComment-1244987 Share on other sites More sharing options...
xcandiottix Posted July 20, 2011 Author Share Posted July 20, 2011 $liveArray = array(); //Add needed data to JSON creation with required html $liveArray['ID'] = $ID; $liveArray['current'] = number_format(($checkPrice - $deduct),2); $savings = 1-@($checkPrice / $originalPrice); $savings = round($savings, 4); $liveArray['currentSavings'] = $savings*100; $liveArray['place'] = "ON"; $liveArray['buy'] = "OFF"; if($Status == "ON"){ $liveArray['pb'] = "ON"; } else{ $liveArray['pb'] = "OFF"; } $data = json_encode($liveArray); $liveArray['Stream'] = $_GLOBALS['History']; $data = json_encode($liveArray); $myFile = '../json/auctions/'.$ID.'-live.js'; $fh = fopen($myFile, 'w') or die('can\'t open file'); fwrite($fh, $data); fclose($fh); //Set file access chmod('../json/auctions/'.$ID.'-live.js', 0644); //TESTING echo $data; unset($liveArray, $data, $myFile, $fh); Sorry, I didn't show the array construction. Stream already part of the array but it's forcing quotes after the : and before the [ if that makes sense. That's the code that creates the JSON. There's some other things being added to the JSON but they don't affect the problem I'm having. To simplify my issue, I'm using php to create liveArray with a few different json object pairs (name:value). At the end I want to attach an array (name:["value 1","value 2","value 3"]). The problem is that once the array is created, there is an error in the way the array is entered into the json file. The error being that the json_encode() command is placing a double quote BEFORE the opening bracket of the array (name:"["value1...). The double quote between the : [ causes the following double quotes around the array vaules gets escaped (name:"[\"value 1\"]) therefore it cannot be parsed later on when I call it to populate a div. Quote Link to comment https://forums.phpfreaks.com/topic/242404-php-to-json-issue/#findComment-1244999 Share on other sites More sharing options...
dcro2 Posted July 20, 2011 Share Posted July 20, 2011 You did show the array construction, but what I'm saying it don't build it yourself! Make $_GLOBALS['History'] a PHP array. json_encode will take that array and make it proper JSON like you want it. Right now it's a string, so json_encode converts it to JSON as a string. It doesn't check what's inside to see if you just decided to make your own JSON array. For example, if this was the contents of $liveArray: $array['Stream'] = array('1', '2', '3'); /*Array ( [stream] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )*/ Then json_encode will take that array and convert it into this: {"Stream":["1","2","3"]} I would just do this: $_GLOBALS['History'] = array(); while($row3 = @mysql_fetch_array($result3)){ $number = $row3['db_Id']; $time = $row3['db_Timestamp']; date_default_timezone_set('America/Phoenix'); $time = date("l M j g:i:s a T Y", $time); $_GLOBALS['History'][] = "<img src='../images/avatar/".md5($row['db_UserId']).".jpg'>".$row['db_UserId']."<br>$time<br>"; unset($alert); } Quote Link to comment https://forums.phpfreaks.com/topic/242404-php-to-json-issue/#findComment-1245129 Share on other sites More sharing options...
xcandiottix Posted July 21, 2011 Author Share Posted July 21, 2011 Sorry for the misunderstanding initially dcro2. Your way seems to work perfectly. Thank you very much for your assistance! Quote Link to comment https://forums.phpfreaks.com/topic/242404-php-to-json-issue/#findComment-1245483 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.