简体   繁体   中英

How can we convert the data received into JSON format in Python

I am getting a data using GET method in Python and again i am posting the data i got to another URL using POST method using python Script. But while posting I am getting ERROR 400 BAD REQUEST - which I think because of the data format i am sending to the URL.

The Post URL I am using will accept J SON format.The data I got is in TYPE DICT. Can you give me some example for how to convert the data and send to URL?

尝试这样的事情,让我知道,

requests.post(url, data=json.dumps(dict_data))

I think this is what you are looking for:

import json

artistDict = {
  'name'     : 'Davy Jones',
  'band'     : 'Monkeys',
  'Genre'    : 'Rock',
  'firstYear': 1966
}
artist_json = json.dumps(artistDict)
print(artist_json)

will produce

{"name": "Davy Jones", "band": "Monkeys", "Genre": "Rock", "firstYear": 1966}

You would then be able to send artist_json to your receiving application

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