简体   繁体   中英

JSON for high chart imported with json.load generated error when using json.dump

I am attempting to write a JSON file for highcharts-convert/phantomJS dynamically with python. I have a valid original.json that works. When i use this simple program to bring it in with json.load and then json.dump it to data.json - the data.json generates an error!

import json

with open('orignal.json') as data_file:
    data = json.load(data_file)

with open('data.json', 'w') as outfile:
    json.dump(data, outfile, indent=4, separators=(',',':'), ensure_ascii=True)

Here is the Error I get when phantomjs/highcharts-convert.js reads it:

 SyntaxError: Unexpected token ':'
TRACE:
 -> phantomjs://code/data.js: 2 (in function "injectJs")
 -> phantomjs://code/highcharts-convert.js: 637 (in function "injectResources")
 -> phantomjs://code/highcharts-convert.js: 808
 -> phantomjs://platform/webpage.js: 286 (in function "_onPageOpenFinished")

  phantomjs://code/highcharts-convert.js:723 in onError
Exited with message 'ERROR: SyntaxError: Unexpected token ':''

Here is the original.json: http://pastebin.com/fAXtmHTc

and here is the json.dump data.json: http://pastebin.com/mXjeNYug What went wrong? Other than order, they seem the same!

You are executing this command:

phantomjs highcharts-convert.js -infile data.json -outfile chart.png

According to the docs (search for "legacy solution", the -infile parameter should be:

The file to convert, the script have to find if this is a javascript file with a options object or a svg file. It checks the input file for beginning with "<svg", "<?xml" or "<!doctype". Then it's a svg file, otherwise it's presumed to be an options file.

but you are passing it JSON , which it can't parse.

Try passing a javascript file like this:

var options = { // your JSON here, as a js object };

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