Jump to content

PHP to JSON issue


xcandiottix

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.