i want to pass a json object which contains nested objects from my client to my server.
我想传递一个json对象,其中包含从我的客户端到我的服务器的嵌套对象。
on the client side, my data structure looks like this:
在客户端,我的数据结构如下所示:
var response = {};
response['screening'] = '1';
response['assistance'] = 'wheelchair access';
response['guests'] = {};
response['guests']['1'] = {}
response['guests']['1']['first'] = 'John'
response['guests']['1']['last'] = 'Smith'
response['guests']['2'] = {}
response['guests']['2']['first'] = 'Dave'
response['guests']['2']['last'] = 'Smith'
and my ajax call looks like this:
我的ajax调用看起来像这样:
$.ajax({
type: "POST",
url: window.location.pathname,
data: response
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
after posting this data to my server, which is run using python flask, i use the request.form object to inspect what was posted from the client. i'd like the data to be structured the same way, however, this is the output on the server:
在将此数据发布到使用python flask运行的服务器之后,我使用request.form对象来检查从客户端发布的内容。我想要以相同的方式构建数据,但是,这是服务器上的输出:
ImmutableMultiDict([('guests[1][first]', u'John'), ('screening', u'2'), ('guests[2][last]', u'Smith'), ('guests[2][first]', u'Dave'), ('assistance', u'wheelchair access'), ('guests[1][last]', u'Smith')])
as you can see, the response['guests'] object got flattened, and all of its children, such as:
正如您所看到的,响应['guests']对象变得扁平化,并且它的所有子节点,例如:
'guests[2][first]'
的客人[2] [第一]'
... are just a strings, not elements of their parent response['guests'].
......只是一个字符串,而不是父母反应的元素['来宾']。
is there a better way to send this block of data from my client to my server, and maintain its structure properly?
有没有更好的方法将这个数据块从我的客户端发送到我的服务器,并正确维护其结构?
thanks!
谢谢!
2 个解决方案
#1
12
You could send your object as a JSON string:
您可以将对象作为JSON字符串发送:
var data = {
screening: '1',
assistance: 'wheelchair access',
guests: [
{
first: 'John',
last: 'Smith'
},
{
first: 'Dave',
last: 'Smith'
}
]
};
$.ajax({
type: 'POST',
url: window.location.href,
data: JSON.stringify(response),
dataType: 'json',
contentType: 'application/json; charset=utf-8'
}).done(function(msg) {
alert("Data Saved: " + msg);
});
And then use request.json
to access it.
然后使用request.json访问它。
#2
0
On the client side, you need to convert that javascript object to a json string. To do so, you can use this:
在客户端,您需要将该javascript对象转换为json字符串。为此,您可以使用:
JSON.stringify(my_object) // This will return a string that you can pass in you ajax request
Then on the server side, you need to convert that object to a python dictionnary using the json module:
然后在服务器端,您需要使用json模块将该对象转换为python dictionnary:
import simplejson
my_new_object = simplejson.loads(my_json) // my_json is my_object from the client (previously called my_object)
my_new_object is now a python dictionnary, and you can do whatever you want with it
my_new_object现在是一个python dictionnary,你可以随心所欲地做任何事情
#1
12
You could send your object as a JSON string:
您可以将对象作为JSON字符串发送:
var data = {
screening: '1',
assistance: 'wheelchair access',
guests: [
{
first: 'John',
last: 'Smith'
},
{
first: 'Dave',
last: 'Smith'
}
]
};
$.ajax({
type: 'POST',
url: window.location.href,
data: JSON.stringify(response),
dataType: 'json',
contentType: 'application/json; charset=utf-8'
}).done(function(msg) {
alert("Data Saved: " + msg);
});
And then use request.json
to access it.
然后使用request.json访问它。
#2
0
On the client side, you need to convert that javascript object to a json string. To do so, you can use this:
在客户端,您需要将该javascript对象转换为json字符串。为此,您可以使用:
JSON.stringify(my_object) // This will return a string that you can pass in you ajax request
Then on the server side, you need to convert that object to a python dictionnary using the json module:
然后在服务器端,您需要使用json模块将该对象转换为python dictionnary:
import simplejson
my_new_object = simplejson.loads(my_json) // my_json is my_object from the client (previously called my_object)
my_new_object is now a python dictionnary, and you can do whatever you want with it
my_new_object现在是一个python dictionnary,你可以随心所欲地做任何事情