我有一个HTML代码:
<button>asd</button>
<script type = "text/javascript">
$('button').click(
function() {
$.getJSON('/schedule/test/', function(json) {
alert('json: ' + json + ' ...');
});
}
);
</script>
Run Code Online (Sandbox Code Playgroud)
和相应的观点:
def test(request):
if request.method == 'GET':
json = simplejson.dumps('hello world!')
return HttpResponse(json, mimetype = 'application/json')
Run Code Online (Sandbox Code Playgroud)
执行视图(使用测试print),json初始化变量但不显示警报.我做错了什么?我已经看过一些关于此的文档(http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback例如)但我没有找到答案.
编辑:问题是,这HttpResponse是没有导入...不幸的是Django没有给出任何错误.其他一切都是正确的.尊重
克里斯
使用时
from django.utils import simplejson
Run Code Online (Sandbox Code Playgroud)
从db.Model它派生的类型的对象抛出异常.如何规避这个?
我从Instagram API获得以下数据,我试图property从caption使用以下代码获取文本:
data = simplejson.load(info) # info is retrieved using the urllib2
for post in data['data']:
if post['caption'] is not "null":
try:
post['caption']['text']
except NameError:
post['caption']['text'] = 0
if post['caption']['text'] is not 0:
print post['caption']['text']
Run Code Online (Sandbox Code Playgroud)
但是在打印unicode字符串时我一直收到TypeError: 'NoneType' object has no attribute '__getitem__'错误+ UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: cha
racter maps to <undefined>错误
以下是检索和存储的JSON数据 info
{
"pagination":{
"next_url":"https:\/\/api.instagram.com\/v1\/users\/self\/feed?access_token=184046392.f59def8.c5726b469ad2462f85c7cea5f72083c0&count=3&max_id=247821697921944007_6064449",
"next_max_id":"247821697921944007_6064449"
},
"meta":{
"code":200
},
"data":[
{
"attribution":null,
"tags":[
"usausausa", …Run Code Online (Sandbox Code Playgroud) 我试图转换以下内容dict:
{
'post_engaged': 36,
'post_impressions': 491,
'post_story': 23,
'comment_count': 6,
'created_time': '03:02 AM, Sep 30, 2012',
'message': 'Specialities of Shaktis and Pandavas. \n While having power, why there isn\\u2019t',
< built - in function id > : '471662059541196',
'status_type': 'status',
'likes_count': 22
} {
'post_engaged': 24,
'text': '30 Sept 2012 Avyakt Murlli ( Dual Voice )',
'post_story': 8,
'comment_count': 3,
'link': 'http:\\/\\/www.youtube.com\\/watch?v=VGmFj8g7JFA&feature=youtube_gdata_player',
'post_impressions': 307,
'created_time': '03:04 AM, Sep 30, 2012',
'message': 'Not available',
< built - in function …Run Code Online (Sandbox Code Playgroud) 我使用带有simplejson的Python将多个嵌套字典序列化为JSON.
有没有办法自动排除空/空值?
例如,序列化:
{
"dict1" : {
"key1" : "value1",
"key2" : None
}
}
Run Code Online (Sandbox Code Playgroud)
至
{
"dict1" : {
"key1" : "value1"
}
}
Run Code Online (Sandbox Code Playgroud)
使用Jackson和Java时,您可以使用它Inclusion.NON_NULL来执行此操作.有一个简单的json等价物吗?
我正在尝试在Django(1.7.1)中构建一个实时聊天应用程序.看来我需要安装Redis和ishout.js.所以我按照说明安装了它们.
在Django中创建项目后,我将'drealtime'放在INSTALLED_APPS下,然后放入:
'drealtime.middleware.iShoutCookieMiddleware'
Run Code Online (Sandbox Code Playgroud)
正上方 :
'django.contrib.sessions.middleware.SessionMiddleware'
Run Code Online (Sandbox Code Playgroud)
下的MIDDLEWARE_CLASSES,因为它在说什么.我把命令就像
python manage.py startapp example
Run Code Online (Sandbox Code Playgroud)
但我仍然有这个导入错误消息:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
module = import_module(entry)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/drealtime/__init__.py", line 4, in <module>
from django.utils …Run Code Online (Sandbox Code Playgroud) 我有一个dict,我想使用simplejson转换为JSON.
如何确保我的dict的所有键都是小写的?
{
"DISTANCE": 17.059918745802999,
"name": "Foo Bar",
"Restaurant": {
"name": "Foo Bar",
"full_address": {
"country": "France",
"street": "Foo Bar",
"zip_code": "68190",
"city": "UNGERSHEIM"
},
"phone": "+33.389624300",
"longitude": "7.3064454",
"latitude": "47.8769091",
"id": "538"
},
"price": "",
"composition": "",
"profils": {},
"type_menu": "",
"ID": ""
},
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢大家看看我的问题,对不起,我没有详细解释为什么我想要这个.这是修补JSONEmitter的django-piston.
什么是更快:
(A)'Unpickling'(正在加载)一个酸洗字典对象,使用 pickle.load()
要么
(B)使用将JSON文件加载到字典 simplejson.load()
假设:在案例A中已存在pickle对象文件,并且在案例B中已存在JSON文件.
我已经理解了simplejson比Python中的原生json快得多,例如这个线程: json和simplejson Python模块之间有什么区别?
但是,当我阅读使用Python 2.7的App Engines文档时,我只是被抛出一个循环
使用原生JSON库,比simplejson快得多.
http://code.google.com/appengine/docs/python/python27/newin27.html
所以现在我很困惑.其他地方似乎都说simplejson更好,但是现在App Engine和Python 2.7说本机更快.是什么赋予了?
我注意到,单引号引起simplejson的loads功能失效:
>>> import simplejson as json
>>> json.loads("\"foo\"")
'foo'
>>> json.loads("\'foo\'")
Traceback (most recent call last):
...
ValueError: No JSON object could be decoded
Run Code Online (Sandbox Code Playgroud)
我正在解析这样的事情:foo = ["a", "b", "c"]从文本文件到Python中的列表,并且也想接受foo = ['a', 'b', 'c'].simplejson方便foo自动进入列表.
如何loads在不破坏输入的情况下接受单引号,或自动将双引号替换为单引号?谢谢.
simplejson ×10
python ×9
json ×8
django ×2
django-1.7 ×1
getjson ×1
jquery ×1
pickle ×1
python-2.7 ×1
unicode ×1