如何在谷歌应用程序引擎中解析JSON ?

时间:2022-05-27 07:27:38

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 recommend built in, or a library that I have to include in my app? Is it secure? Thanks.

我想将JSON字符串解析为谷歌应用程序引擎(python)下的对象。你推荐什么?可以编码/stringify也不错。你推荐的是内置的,还是我的应用程序中必须包含的一个库?它是安全的吗?谢谢。

6 个解决方案

#1


113  

Consider using Django's json lib, which is included with GAE.

考虑使用Django的json库,它包含在GAE中。

from django.utils import simplejson as json

# load the object from a string
obj = json.loads( string )

The link above has examples of Django's serializer, and here's the link for simplejson's documentation.

上面的链接有Django序列化器的示例,下面是simplejson文档的链接。

If you're looking at storing Python class instances or objects (as opposed to compositions of lists, strings, numbers, and dictionaries), you probably want to look at pickle.

如果您希望存储Python类实例或对象(与列表、字符串、数字和字典的组合不同),那么您可能需要查看pickle。

I hope that helps.

我希望有帮助。

Incidentally, to get Django 1.0 (instead of Django 0.96) running on GAE, you can use the following call in your main.py, per this article:

顺便提一下,要让Django 1.0(而不是Django 0.96)在GAE上运行,您可以在您的主服务器中使用以下调用。py,/这篇文章:

from google.appengine.dist import use_library
use_library('django', '1.0')

Edit: Native JSON support in Google App Engine 1.6.0 with Python 2.7

As of Google App Engine 1.6.0, you can use the Python 2.7 runtime by adding runtime: python27 in app.yaml, and then you can import the native JSON library with import json.

在谷歌App Engine 1.6.0中,您可以通过在App .yaml中添加运行时:python27来使用Python 2.7运行时,然后您可以导入JSON的本地库。

#2


21  

Google App Engine now supports python 2.7. If using python 2.7, you can do the following:

谷歌应用引擎现在支持python 2.7。如果使用python 2.7,可以执行以下操作:

import json
structured_dictionary = json.loads(string_received)

#3


6  

Include the simplejson library with your app?

在应用程序中包含simplejson库?

#4


1  

If you're using Python2.6 or greater, I've used with success the built-in json.load function. Otherwise, simplejson works on 2.4 without dependencies.

如果您正在使用Python2.6或更高版本,我已经成功地使用了内置的json。加载函数。否则,simplejson可以在没有依赖项的2.4上工作。

#5


1  

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:

这是一个老问题,但我想我会给出一个更新的,更详细的答案。对于现在登录这里的用户,几乎可以肯定您正在使用python 2.6或更高版本,因此您可以使用python 2的内置json模块(或者python 3的内置json模块,因为谷歌最近在GAE上添加了对python 3的支持)。导入就像导入json一样简单。下面是一些如何使用json模块的示例:

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.

如果您使用的是旧版本的python,请坚持使用@Brian M. Hunt的答案。

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

这里是Python 2的json模块的doc页面,这里是Python 3的doc页面。

#6


0  

Look at the python section of json.org. The standard library support for JSON started at python 2.6, which I believe is newer than what the app engine provides. Maybe one of the other options listed?

请看json.org的python部分。JSON的标准库支持始于python 2.6,我认为它比应用程序引擎提供的更新。也许是列出的其他选项之一?

#1


113  

Consider using Django's json lib, which is included with GAE.

考虑使用Django的json库,它包含在GAE中。

from django.utils import simplejson as json

# load the object from a string
obj = json.loads( string )

The link above has examples of Django's serializer, and here's the link for simplejson's documentation.

上面的链接有Django序列化器的示例,下面是simplejson文档的链接。

If you're looking at storing Python class instances or objects (as opposed to compositions of lists, strings, numbers, and dictionaries), you probably want to look at pickle.

如果您希望存储Python类实例或对象(与列表、字符串、数字和字典的组合不同),那么您可能需要查看pickle。

I hope that helps.

我希望有帮助。

Incidentally, to get Django 1.0 (instead of Django 0.96) running on GAE, you can use the following call in your main.py, per this article:

顺便提一下,要让Django 1.0(而不是Django 0.96)在GAE上运行,您可以在您的主服务器中使用以下调用。py,/这篇文章:

from google.appengine.dist import use_library
use_library('django', '1.0')

Edit: Native JSON support in Google App Engine 1.6.0 with Python 2.7

As of Google App Engine 1.6.0, you can use the Python 2.7 runtime by adding runtime: python27 in app.yaml, and then you can import the native JSON library with import json.

在谷歌App Engine 1.6.0中,您可以通过在App .yaml中添加运行时:python27来使用Python 2.7运行时,然后您可以导入JSON的本地库。

#2


21  

Google App Engine now supports python 2.7. If using python 2.7, you can do the following:

谷歌应用引擎现在支持python 2.7。如果使用python 2.7,可以执行以下操作:

import json
structured_dictionary = json.loads(string_received)

#3


6  

Include the simplejson library with your app?

在应用程序中包含simplejson库?

#4


1  

If you're using Python2.6 or greater, I've used with success the built-in json.load function. Otherwise, simplejson works on 2.4 without dependencies.

如果您正在使用Python2.6或更高版本,我已经成功地使用了内置的json。加载函数。否则,simplejson可以在没有依赖项的2.4上工作。

#5


1  

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:

这是一个老问题,但我想我会给出一个更新的,更详细的答案。对于现在登录这里的用户,几乎可以肯定您正在使用python 2.6或更高版本,因此您可以使用python 2的内置json模块(或者python 3的内置json模块,因为谷歌最近在GAE上添加了对python 3的支持)。导入就像导入json一样简单。下面是一些如何使用json模块的示例:

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.

如果您使用的是旧版本的python,请坚持使用@Brian M. Hunt的答案。

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

这里是Python 2的json模块的doc页面,这里是Python 3的doc页面。

#6


0  

Look at the python section of json.org. The standard library support for JSON started at python 2.6, which I believe is newer than what the app engine provides. Maybe one of the other options listed?

请看json.org的python部分。JSON的标准库支持始于python 2.6,我认为它比应用程序引擎提供的更新。也许是列出的其他选项之一?