简体   繁体   中英

How to send multilevel JSON as GET parameter and decode to dictionary in python?

I would like to send multilevel JSON object as GET request parameter in JavaScript. To serialize I use this:

json_object = {
    key_a: {
        key_aa: 1,
        key_ab: 2,
    },
    key_b: {
        key_ba: 1,
        key_bb: 2,
    },
}
var encoded_json = jQuery.param( json_object );

and receive:

key_a%5Bkey_aa%5D=1&key_a%5Bkey_ab%5D=2&key_b%5Bkey_ba%5D=1&key_b%5Bkey_bb%5D=2

So, next, in python I would like to decode this string:

print(parse_qs(json))

and I receive:

{
    'key_a[key_aa]': ['1'], 
    'key_a[key_ab]': ['2'], 
    'key_b[key_ba]': ['1'], 
    'key_b[key_bb]': ['2']
}

How can I transform this dictionary to form similar to input JSON object?

The answer of my question is: use JSON.stringify

serialized_object = JSON.stringify(object) 

to serialize JavaScript object and next, use

json.loads(serialized_object)

in Python to get dictionary in case of POST request using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM