How to externalize JSON
I have the following JSON
in my view (simplified for this example):
video =
{
'label': 'hd',
'url': 'google',
'format': 'mp4',
'video_codec': 'h264',
'audio_codec': 'aac',
'size': '1080x720',
}
And I can call a function like so: do_something(video)
However, when I try and extract this JSON
into an external file, as follows --
file=open('data.json')
video=file.read()
I get an error, which appears to be related to having newlines and excessive spaces in the file开发者_如何学运维. How would I 1) format the above json document to put it into an external file; and 2) how would I import it so I can use it with a function? Thank you.
It's easy once you have actual JSON, preferably from json.dump()
.
$ cat t.json
{
"label": "hd",
"url": "google",
"format": "mp4",
"video_codec": "h264",
"audio_codec": "aac",
"size": "1080x720"
}
$ python << EOF
> import json
> f = open('t.json')
> print json.load(f)
> f.close()
> EOF
{u'format': u'mp4', u'url': u'google', u'label': u'hd', u'audio_codec': u'aac', u'video_codec': u'h264', u'size': u'1080x720'}
精彩评论