jquery中json数据转换为字典
生活随笔
收集整理的這篇文章主要介紹了
jquery中json数据转换为字典
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先在前臺(tái)頁面中的json數(shù)據(jù)為 var recipe = {};recipe["name"] = $("#name").val();recipe["age"] = $("#age").val();recipe["sex"] = $("#sex").val();recipe["medicine"] = "{'name': 'a', 'value': 'b'}";
在django的view中獲取
能夠發(fā)現(xiàn)medicine下的數(shù)據(jù)為字符串,可是是unciode格式
if request.method == "POST":recipe_json = request.POST['medicine']//首先這里獲取到的數(shù)據(jù)為unicode格式recipe = recipe_json.encode("utf8")//將其轉(zhuǎn)化為strprint recipeprint eval(recipe)//用eval方法轉(zhuǎn)化為字典recipe_dict = eval(recipe)print recipe_dict['name']return HttpResponse(json.dumps(True))
使用ajax的post進(jìn)行傳輸
$.ajax({type: "post",dataType: "json",url: "/doctor/recipe/add/",data: recipe,success: function(Data){alert("success")}});在django的view中獲取
request.POST中的數(shù)據(jù)為
<QueryDict: {u'medicine': [u"{'name': 'a', 'value': 'b'}"], u'age': [u'12'], u'name': [u'asdf'], u'sex': [u'1']}>能夠發(fā)現(xiàn)medicine下的數(shù)據(jù)為字符串,可是是unciode格式
if request.method == "POST":recipe_json = request.POST['medicine']//首先這里獲取到的數(shù)據(jù)為unicode格式recipe = recipe_json.encode("utf8")//將其轉(zhuǎn)化為strprint recipeprint eval(recipe)//用eval方法轉(zhuǎn)化為字典recipe_dict = eval(recipe)print recipe_dict['name']return HttpResponse(json.dumps(True))
轉(zhuǎn)載于:https://www.cnblogs.com/liguangsunls/p/6708353.html
總結(jié)
以上是生活随笔為你收集整理的jquery中json数据转换为字典的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页版excel数据批量导入数据库
- 下一篇: android 解析网络数据(JSON)