有效的JSON不能与jQuery.parseJSON()一起使用

时间:2013-06-13 21:13:21

标签: php javascript json

我正在将JSON字符串保存到文件并尝试将其读回。由于某种原因,它不会读回来。 jsonlint.com告诉我它是一个有效的JSON。

这是JSON字符串:

{"userdef":{"vlan10":{"dfault":{"down":{"rate":"876","ceil":"876"},"up":{"rate":"876","ceil":"876"}},"upsell":{"down":{"rate":"876","ceil":"876"},"up":{"rate":"876","ceil":"76"}}},"br0":{"dfault":{"down":{"rate":"798","ceil":"987"},"up":{"rate":"987","ceil":"987"}},"upsell":{"down":{"rate":"98","ceil":"987"},"up":{"rate":"987","ceil":"89"}}},"br1":{"dfault":{"down":{"rate":"3","ceil":"654"},"up":{"rate":"654","ceil":"63"}},"upsell":{"down":{"rate":"65","ceil":"4"},"up":{"rate":"646","ceil":"5"}}},"eth3":{"dfault":{"down":{"rate":"65","ceil":"7876"},"up":{"rate":"7657","ceil":"5"}},"upsell":{"down":{"rate":"7865","ceil":"7"},"up":{"rate":"7","ceil":"5"}}}}}

这是javascript / php代码:

<?
if (file_exists('/tmp/qosconfig.conf'))
{
?>
var config = jQuery.parseJSON('<?=file_get_contents("/tmp/qosconfig.conf");?>');    
<?
}
?>

1 个答案:

答案 0 :(得分:0)

这是一种相当奇怪的方式。如果您在PHP中可以使用JSON字符串,则可以将其作为对象文字输出到javascript中,从而保存解析步骤。

<?
if (file_exists('/tmp/qosconfig.conf')) {
    $json = file_get_contents('/tmp/qosconfig.conf');
?>
var config = <?php echo $json; ?>;    
<?
}
?>