I'm trying to build a PHP form that outputs custom JSON code.
Take a look: http://s194239704.onlinehome.us/bcembed/
The JSON code output that the app creates is wrong. I need to do a search and replace to remove some of the commas.
The (partial) source code looks like this:
{
<!-- ALBUM ART --><span <?php if($artdisplay!="block") echo "style=\"display:none;\""; ?>>"art": { "x": <?php echo $artx; ?>, "y": <?php echo $arty; ?>, "w": <?php if($artsize=="small") {echo "100";} elseif($artsize=="large") {echo "150";} ?>, "h": <?php if($artsize=="small") {echo "100";} elseif($art开发者_StackOverflow中文版size=="large") {echo "150";} ?>, "show": true },</span>
<!-- MAINTEXT --><span <?php if($maintextdisplay!="block") echo "style=\"display:none;\""; ?>>"maintext": { "x": <?php echo $maintextx; ?>, "y": <?php echo $maintexty; ?>, "w": <?php echo $maintextw; ?>, "h": <?php echo $maintexth; ?>, "show": true, "styles": { "fontSize": "<?php echo $maintextfontsize; ?>", "textAlign": "<?php echo $maintextalign; ?>", <?php if($maintextbold=="bold") echo "\"fontWeight\": \"" . $maintextbold . "\","; ?> <?php if($maintextitalic=="italic") echo "\"fontStyle\": \"" . $maintextitalic . "\","; ?> }},</span>
}
I want to run the search/replace after the PHP is applied. I tried wrapping the whole thing in a JavaScript search/replace, because I thought the PHP would run before the Javascript code. But nothing happened.
Can you tell I'm in over my head? Half-assed copy and pasting can only get me so far...
Edit: I didn't know about json_encode. It seems to be working, but I ran into another snag. I want to have this as the output:
"currenttime": {
"x": 0,
"y": 0,
"w": 30,
"h": 30,
"show": true,
"styles": {
"fontSize": "13",
"fontWeight": "bold",
"fontStyle": "null",
"textAlign": "center"
}
}
And this is the code I'm trying to use:
$jsonData['currenttime'] = array(
'x' => $currenttimex,
'y' => $currenttimey,
'w' => $currenttimew,
'h' => $currenttimeh,
'show' => $currenttimedisplay=="block" ? true : false,
['styles'] = array(
'fontSize' => $currenttimefontsize,
'fontWeight' => $currenttimebold,
'fontStyle' => $currenttimeitalic,
'textAlign' => $currenttimealign
)
);
It's like I need a sub-array for the styles... what's the right way to format this?
Youre essentially ok, but you have some syntax mistakes:
$jsonData['currenttime'] = array(
'x' => $currenttimex,
'y' => $currenttimey,
'w' => $currenttimew,
'h' => $currenttimeh,
'show' => $currenttimedisplay =="block" ? true : false,
'styles' => array(
'fontSize' => $currenttimefontsize,
'fontWeight' => $currenttimebold,
'fontStyle' => $currenttimeitalic,
'textAlign' => $currenttimealign
)
);
I wouldnt manually with this... use json_encode
instead:
$jsonData = array();
$jsonData['art'] = array(
'x' => $artx,
'y' => $arty,
'w' => $artsize=="small" ? 100 : ($artsize == 'large' ? 150 : null),
'h' => $artsize=="small" ? 100 : ($artsize == 'large' ? 150 : null),
'show' => true
);
echo json_encode($jsonData);
精彩评论