javascript - Django json single and double quotes? - Stack Overflow

I noticed that if you use the simplejson function in django all the strings are enclosed with single qu

I noticed that if you use the simplejson function in django all the strings are enclosed with single quotes, and the whole json object string is enclosed in double quotes. When I take this string and hand it in to JSON.parse, it gives me an error because they want to use single quotes to enclose the whole object and double quotes for the strings. I could switch them with javascript replace, but then I'd have to take into consideration cases like apostrophes, but I'm sure there is a better way. Is there a way to get django's simplejson to output the object string to the format of JSON.parse?

More info:

django view:

def view(request):
    list = [{"a":"apple",},]
    return HttpResponse(simplejson.dumps(str(list)), mimetype="application/json")

what the javascript string turn out to be

"[{'a': 'apple'}]"

I noticed that if you use the simplejson function in django all the strings are enclosed with single quotes, and the whole json object string is enclosed in double quotes. When I take this string and hand it in to JSON.parse, it gives me an error because they want to use single quotes to enclose the whole object and double quotes for the strings. I could switch them with javascript replace, but then I'd have to take into consideration cases like apostrophes, but I'm sure there is a better way. Is there a way to get django's simplejson to output the object string to the format of JSON.parse?

More info:

django view:

def view(request):
    list = [{"a":"apple",},]
    return HttpResponse(simplejson.dumps(str(list)), mimetype="application/json")

what the javascript string turn out to be

"[{'a': 'apple'}]"

Share edited Apr 12, 2012 at 2:42 Derek asked Apr 12, 2012 at 1:57 DerekDerek 12.4k31 gold badges106 silver badges166 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

update remove the str() around list, simply simplejson.dumps(list). str() trans the list to a string, thus you got "[{'a': 'apple'}]" in client side.

Can you update the question to demo where simplejson encloses strings w/ single quotes?

django.utils.simplejson, normally, conforms w/ JSON specification and does not use single quotes to wrap things. If you mean

>>> from django.utils.simplejson import dumps
>>> dumps("Hello")
'"Hello"' # single quotes here
>>> repr(dumps("Hello"))
'\'"Hello"\'' # or here

They're notations of Python, you don't want to directly use them in JSON.parse (the first one is OK though).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745046704a4608140.html

相关推荐

  • javascript - Django json single and double quotes? - Stack Overflow

    I noticed that if you use the simplejson function in django all the strings are enclosed with single qu

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信