简体   繁体   中英

Valid JSON not working with jQuery.parseJSON()

I'm saving a JSON string to a file and trying to read it back. For some reason it won't read it back. jsonlint.com is telling me it is a valid JSON.

Here is the JSON string:

{"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"}}}}}

Here is javascript/php code:

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

This is a rather odd way of doing this. If you have the JSON string available to you in PHP, you can output it into the javascript as an object literal which saves the parsing step.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM