我正在尝试将json结果存储在GAE数据存储区中,以便我以后可以读取它.我将它转储到String,然后存储它,然后读取它并将其加载回dict.但是在装载之后我再也不能把它看成dict了.
result = freebase.mqlready(query)
Run Code Online (Sandbox Code Playgroud)
打印结果:
[{u'mid': u'/m/095hd',
u'name': u'Settlers of Catan',
u'type': u'/games/game'},
{u'mid': u'/m/025sm93',
u'name': u'The Game of Life',
u'type': u'/games/game'}]
Run Code Online (Sandbox Code Playgroud)
for r in result:
name = r.name # works, I can get the name and other values.
json_dump = simplejson.dumps(result)
text = db.Text(json_dump)
fbresult = model.FB(text=text)
fbresult.put()
####
stored_text = fbresult.text
json = simplejson.loads(stored_text)
Run Code Online (Sandbox Code Playgroud)
打印json:
[{u'mid': u'/m/095hd',
u'name': u'Settlers of Catan',
u'type': u'/games/game'},
{u'mid': u'/m/025sm93',
u'name': u'The Game of Life',
u'type': u'/games/game'}]
Run Code Online (Sandbox Code Playgroud)
for j in json: …
Run Code Online (Sandbox Code Playgroud) 我正在使用Django加载JSON字符串simplejson
,因此:
obj = json.loads('{"name": "joe"}')
person = obj.name
Run Code Online (Sandbox Code Playgroud)
这会引发错误:
'dict' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)
但是当我传递obj
给视图模板并通过{{ obj.name }}
它打印出来时,它可以工作!为什么?
在这里,我想将一个"大"字典转储到json中,如下所示:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import simplejson as json
doc = {}
# appending the doc, so that the doc is more than 2G
.....
json_doc = json.dumps(doc)
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误消息:
File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 286, in dump
s
return _default_encoder.encode(obj)
File "C:\Python27\lib\site-packages\simplejson\encoder.py", line 228, in encod
e
chunks = list(chunks)
MemoryError
Run Code Online (Sandbox Code Playgroud)
我该如何解决?谢谢!
我已经搜索了这个,但是我找到的所有结果对我来说都没有意义,看起来太复杂了.我想使用json
或simplejson
模块来获取对象中字符串的值.
string = '{"name": "Alex"}'
Run Code Online (Sandbox Code Playgroud)
基本上,我想从那里提取'Alex'的值.我该怎么做才能做到这一点?
那么,2个直截了当的问题.我正在尝试使用json和django.我试图使用shell并导入json没有错误.但是当我尝试使用json_dumps()或者只是转储()时,我得到了
NameError: name 'json_dumps' is not defined
Run Code Online (Sandbox Code Playgroud)
显然我的json导入尽管没有错误但仍无法正常工作.
此外,我在此期间提出的一些奖励问题是:
使用simpleJSON的JSON序列化Python
如何创建对象以便我们可以优化对象的序列化
我正在使用simpleJSON
1,2是固定变量
3是类别和分数的固定字典
图4是长度固定的dicts阵列(4),该阵列是在运行时特定的长度.
这个过程需要尽可能快,所以我不确定最好的解决方案.
{
"always-include": true,
"geo": null,
"category-score" : [
{
"Arts-Entertainment": 0.72,
"Business": 0.03,
"Computers-Internet": 0.08,
"Gaming": 0.02,
"Health": 0.02,
}
],
"discovered-entities" : [
{
'relevance': '0.410652',
'count': '2',
'type': 'TelevisionStation',
'text': 'Fox News'
},
{
'relevance': '0.396494',
'count': '2',
'type': 'Organization',
'text': 'NBA'
}
]
],
}
Run Code Online (Sandbox Code Playgroud) 将其显示为 JSON 的最佳方式是什么?
{'foo': 'bar'}
[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
我的部分解决方案:
import json
def json_tuple(*args, **kwargs):
if args:
if kwargs:
return json.dumps(args), json.dumps(kwargs)
return json.dumps(args)
return json.dumps(kwargs)
Run Code Online (Sandbox Code Playgroud)
提供:
>>> json_tuple(1,2,3,4,5, **{'foo': 'bar'})
Run Code Online (Sandbox Code Playgroud)
('[1, 2, 3, 4, 5]', '{"foo": "bar"}')
Run Code Online (Sandbox Code Playgroud)
将args
列表放入kwargs
——例如:在args
钥匙下——是唯一的解决方案吗?
我收到错误:
File "/home/mark/Nova/nova/lib/fields.py", line 1, in <module>
from django.contrib.postgres.fields import JSONField
ImportError: cannot import name 'JSONField'
Run Code Online (Sandbox Code Playgroud)
当我跑python manage.py runserver 0.0.0.0:8888
。虽然我已经simplejson
安装了。
pip freeze
显示,
alabaster==0.7.12
Babel==2.7.0
bcdoc==0.16.0
boto3==0.0.21
botocore==1.0.0b3
certifi==2019.9.11
chardet==3.0.4
defusedxml==0.6.0
Django==1.8.2
django-bulk-update==1.1.4
django-cors-headers==2.0.2
django-facebook==6.0.3
django-pgjsonb==0.0.15
django-redis==4.2.0
django-revproxy==0.9.7
djangorestframework==3.1.3
djangorestframework-httpsignature==1.0.0
docutils==0.15.2
elasticsearch==1.6.0
facebook-sdk==1.0.0
filemagic==1.6
futures==2.2.0
geopy==1.11.0
google-api-python-client==1.5.0
httplib2==0.14.0
httpsig==1.3.0
idna==2.8
Jinja2==2.10.3
jmespath==0.7.1
MarkupSafe==1.1.1
mock==1.0.1
mongoengine==0.10.0
msgpack-python==0.5.6
nose==1.3.7
oauth2client==2.2.0
oauthlib==3.1.0
Pillow==2.8.2
psycopg2==2.8.4
py2neo==2.0.8
pyasn1==0.4.7
pyasn1-modules==0.2.7
pycryptodome==3.9.0
Pygments==2.4.2
PyJWT==1.7.1
pymongo==3.0.3
PySocks==1.7.1
python-dateutil==2.8.0
python-social-auth==0.2.10
python3-memcached==1.51
python3-openid==3.1.0
python3-pika==0.9.14
pytz==2019.3
redis==2.10.3 …
Run Code Online (Sandbox Code Playgroud) 为什么这不起作用?我正在阅读simplejson JsonDecoder,true应该是可解析的并且转换为True.
% python
>>> import simplejson as json
>>> print json.loads({"bool":true})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>>
Run Code Online (Sandbox Code Playgroud)