开发者

How to encapsulate JSON in parentheses?

I have this code :

objects = Event.objects.all()
i = 0
dict = {}
small_dict = {}
for o in objects:
    small_dict = {'id': o.id, 'url': o.url, 'name': o.name, 'image': o.image}
    dict[str(i+1)] = small_dict
    small_dict = {}

return HttpResponse(
    simplejson.dumps(dict),
    content_type = 'application/javascript; c开发者_如何学Pythonharset=utf8'
)

and it gives me this :

{"1": {"url": "http://www.rte.ie/tv/crimecall/", "image": "http://img.rasset.ie/0002c8d0-250.jpg", "id": 2, "name": "Crimecall"}}

How I can further encapsulate it between () parentheses ? Because without them I'm getting error when parsing them in php.


  1. The MIME type of JSON is "application/json".
  2. If you have problems parsing it in PHP, then it's a PHP problem. Don't add parens on the server side, but rather add them before parsing the string in PHP. I guess you know how to concatenate in PHP, right? Anyway, I don't understand what your problem is - don't you use json_decode?


You can do it this way, but it's not viewable in browser now. I'f that's not a problem, here's the code :

callback = request.GET.get('callback', '')
objects = Event.objects.all()
i = 0
dict = {}
small_dict = {}
for o in objects:
    small_dict = {'id': o.id, 'url': o.url, 'name': o.name, 'image': o.image}
    dict[str(i+1)] = small_dict
    small_dict = {}

response = simplejson.dumps(dict)
response = callback + '(' + response + ')';

return HttpResponse(response,
    mimetype ='application/json; charset=utf8')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜