How do I encode a global Python variable with JSON? -
How do I encode a global Python variable with JSON? -
i'm trying figure out how can encode globals json format, i'm not sure if it's possible. i'm using python 2.7.8, , i've tried few different approaches:
# variables value = "string" # string value2 = 0 # integer value3 = true # boolean json.dumps({"key": value, "key2": value2, "key3": value3}, sort_keys=true, indent=4, separators=(',', ': '))
i tried making string first (with , without curly braces, with/without quotations on string value etc.)
json_raw = '{"key": %s, "key2": %d, "key3": %s}' % (value, value2, value3) json.dumps(json_raw, sort_keys=true, indent=4, separators=(',', ': '))
however, each 1 of these attempts error:
attributeerror: 'str' object has no attribute 'dumps'
any ideas?
let me reply code:
value = "string" # string value2 = 0 # integer value3 = true # boolean json_map = {} json_map["some string"] = value json_map["some int"] = value2 json_map["some bool"] = value3 result = json.dumps(json_map)
and result containts '{"some int": 0, "some string": "string", "some bool": true}'
. magic of python dictionaries , json.dumps.
python global-variables
Comments
Post a Comment