开发者

How do I deserialize this JSON object?

import simplejson
from urllib2 import urlopen
from simplejson import loads
from django.core import serializers
content = loads(urlopen('https://graph.facebook.com/1234676502/feed').read())
content =  simplejson.dumps(content,sort_keys=True, indent=4)
print content
json_serializer = serializers.get_serializer("json")()
json_serializer.deserialize(content)

While running the above python code im getting the following error:

Traceback (most recent call last):
  File "/var/www/youedo/test.py", line 22, in <module>
    json_serializer = serializers.get_serializer("json")()
  File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/__init__.py", line 63, in get_serializer
    _load_serializers()
  File "/usr/local/lib/python2.6/dist-p开发者_开发问答ackages/django/core/serializers/__init__.py", line 109, in _load_serializers
    register_serializer(format, BUILTIN_SERIALIZERS[format], serializers)
  File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/__init__.py", line 51, in register_serializer
    module = importlib.import_module(serializer_module)
  File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/xml_serializer.py", line 6, in <module>
    from django.core.serializers import base
  File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/base.py", line 7, in <module>
    from django.db import models
  File "/usr/local/lib/python2.6/dist-packages/django/db/__init__.py", line 14, in <module>
    if not settings.DATABASES:
  File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 38, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

...and when I print the content it prints the JSON string correctly.

Edit:2 import simplejson from urllib2 import urlopen from simplejson import loads from django.core import serializers content = loads(urlopen('https://graph.facebook.com/1234676502/feed').read())

json_serializer = serializers.get_serializer("json")()
json_serializer.get_deserialize(content)

I put this code in my view.py then i got the following error:

AttributeError at /rss

'Serializer' object has no attribute 'get_deserialize'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/rss
Django Version:     1.2.4
Exception Type:     AttributeError
Exception Value:    

'Serializer' object has no attribute 'get_deserialize'


Your error has nothing to with json per se, it clearly states that it cannot import DJANGO_SETTINGS which means that you do not have the django app context in place. The easiest way to go about this is install django extensions which provides the command ./manage.py runscript which runs your script with your django app context.

Django extensions can be found here

Edit:: looking at your second edit, I can see that you are using a method get_deserialize(), the right method should be deserialize(), AFAIK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜