Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33743
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-11-21 08:04 -0800 |
| References | <737ef2a9-f2e0-43fe-86a0-199940be2b69@googlegroups.com> <mailman.157.1353512886.29569.python-list@python.org> |
| Subject | Re: Constructing JSON data structures from non-string key python dictionaries |
| From | hfolch@gmail.com |
| Message-ID | <mailman.158.1353513898.29569.python-list@python.org> (permalink) |
Thanks for your reply, but the javascript function expects option names to be unquoted, otherwise it won't work.
On Wednesday, November 21, 2012 3:48:07 PM UTC, MRAB wrote:
> On 2012-11-21 14:59, saikari78 wrote:
>
> > Hi,
>
> >
>
> > I'm using the json module to create a JSON string, then inserting that string into a html template containing a javascript function (from the highcharts library: http://www.highcharts.com/)
>
> > The json string I'm trying to create is to initialize a data variable in the javascript function, that has the following example format.
>
> >
>
> >
>
> >
>
> > data = [{
>
> > y: 55.11,
>
> > color: colors[0],
>
> > drilldown: {
>
> > name: 'MSIE versions',
>
> > categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],
>
> > data: [10.85, 7.35, 33.06, 2.81],
>
> > color: colors[0]
>
> > }
>
> > }]
>
> >
>
> > However, I don't know how to do that because dictionary keys in python need to be strings. If I try to do the following, Python,of course, complains that y,color,drilldown, etc are not defined.
>
> >
>
> >
>
> > import json
>
> >
>
> > data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ]
>
> >
>
> > data_string = json.dumps(data)
>
> >
>
> >
>
> > Many thanks for any suggestions on how to do this.
>
> >
>
> Just quote them:
>
>
>
> data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE
>
> versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE
>
> 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ]
>
>
>
> Incidentally, dictionary keys in Python don't have to be strings, but
>
> merely 'hashable', which includes integers, floats and tuples amongst
>
> others.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Constructing JSON data structures from non-string key python dictionaries saikari78 <saikari78@gmail.com> - 2012-11-21 06:59 -0800
Re: Constructing JSON data structures from non-string key python dictionaries MRAB <python@mrabarnett.plus.com> - 2012-11-21 15:48 +0000
Re: Constructing JSON data structures from non-string key python dictionaries Grant Edwards <invalid@invalid.invalid> - 2012-11-21 16:04 +0000
Re: Constructing JSON data structures from non-string key python dictionaries hfolch@gmail.com - 2012-11-21 08:04 -0800
Re: Constructing JSON data structures from non-string key python dictionaries MRAB <python@mrabarnett.plus.com> - 2012-11-21 16:27 +0000
Re: Constructing JSON data structures from non-string key python dictionaries MRAB <python@mrabarnett.plus.com> - 2012-11-21 16:43 +0000
Re: Constructing JSON data structures from non-string key python dictionaries Chris Rebert <clp2@rebertia.com> - 2012-11-21 11:04 -0800
Re: Constructing JSON data structures from non-string key python dictionaries saikari78 <saikari78@gmail.com> - 2012-11-22 09:05 -0800
Re: Constructing JSON data structures from non-string key python dictionaries saikari78 <saikari78@gmail.com> - 2012-11-22 09:05 -0800
Re: Constructing JSON data structures from non-string key python dictionaries Paul Kölle <paul@subsignal.org> - 2012-11-22 09:53 +0100
Re: Constructing JSON data structures from non-string key python dictionaries hfolch@gmail.com - 2012-11-21 08:04 -0800
csiph-web