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')}
})
})
This is the JS-Code and jsData should be send to the Server(Python). On the Server i get something like {'{id:'E1',firstname:'Peter',lastname:'Funny',project: { id: 'P1' },activities: [{ id: 'A1' },{ id: 'A2' }]};':''}
Is there a smart way to get the string 'inner dict' out of the 'outer dict' ?!
Python has a built-in JSON parsing library. Adding import json
provides basic JSON parsing functionality, which can be used as follows:
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.
More information here: http://docs.python.org/2/library/json.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With