How can I parse JSON in Google App Engine?

前端 未结 6 1639
攒了一身酷
攒了一身酷 2020-11-28 20:59

I\'d like to parse a JSON string into an object under Google App Engine (python). What do you recommend? Something to encode/stringify would be nice too. Is what you reco

6条回答
  •  一个人的身影
    2020-11-28 21:38

    This is an old question, but I thought I'd give an updated, more detailed answer. For those landing here now, you are almost certainly using python 2.6 or greater, so you can use the built-in json module for Python 2 (or for Python 3, since Google recently added support for Python 3 on GAE). Importing is as easy as import json. Here are some examples of how to use the json module:

    import json
    
    # parse json_string into a dict
    json_string = '{"key_one": "value_one", "key_two": 1234}'
    json_dict = json.loads(json_string)
    # json_dict: {u'key_two': 1234, u'key_one': u'value_one'}
    
    # generate json from a dict
    json_dict = {'key': 'value', 'key_two': 1234, 'key_three': True}
    json_string = json.dumps(json_dict)
    # json_string: '{"key_two": 1234, "key": "value", "key_three": true}'
    

    If you are using an older version of python, stick to @Brian M. Hunt's answer.

    Again, here is the doc page for the json module for Python 2, and here it is for Python 3.

提交回复
热议问题