var jsData = {
id: 'E1',
firstname: 'Peter',
lastname: 'Funny',
project: { id: 'P1' },
activities: [
{ id: 'A1' },
{ id: 'A2' }
]};
var jsonData = JSON.stringify(jsData);
$('#click').click(function(){
$.ajax({
url: "test/",
type: "POST",
data: jsData,
dataType: "json",
success: function (data){
console.log(data);
},
error:function(){$('#text').html('FATAL_ERROR')}
})
})
这是 JS 代码,jsData 应该发送到服务器(Python)。 在服务器上,我得到类似 {'{id:'E1',firstname:'Peter',lastname:'Funny',project: { id: 'P1' },activities: [{ id: 'A1' },{ id: 'A2' }]};':''}
是否有一种聪明的方法可以将字符串“inner dict”从“outer dict”中取出?!
请您参考如下方法:
Python 有一个内置的 JSON 解析库。添加import json提供基本的JSON解析功能,使用方法如下:
import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur
person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
