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.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to