Stringify JSON - > Python Server使用字符串化字符串获取dict

Shu*_*ftY 1 python string json

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')}

    })
})
Run Code Online (Sandbox Code Playgroud)

这是JS-Code,jsData应该发送到服务器(Python).在服务器上我得到类似{'{id:'E1',名字:'彼得',姓氏:'搞笑',项目:{id:'P1'},活动:[{id:'A1'},{ id:'A2'}]};':''}

是否有一种聪明的方法可以将字符串"内部字典"从"外部字典"中删除?

Ste*_*ijl 6

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.
Run Code Online (Sandbox Code Playgroud)

更多信息请访问:http: //docs.python.org/2/library/json.html