简体   繁体   中英

json.load changing the string that is input

Hi I am working on a simple program that takes data from a json file (input through an html form with flask handling the data) and uses this data to make calls to an API.

So I have some JSON like this:

[{"id": "ßLÙ", "server": "NA"}]

and I want to send the id to an api call like this example:

http://apicallnamewhatever+id=ßLÙ

however when i load the json file into my app.py with the following command

ids = json.load(open('../names.json'))

json.load seems to alter the id from 'ßLÙ' to 'ßLÙ'

im not sure why this happens during json.load, but i need to find a way to get 'ßLÙ' into the api call instead of the deformed 'ßLÙ'

It looks as if your names.json is encoded in "utf-8" , but you are opening it as "windows-1252" [*] or something like that. Try

json.load(open('names.json', encoding="utf-8"))

and you probably should also URL-encode the id instead of concatenating it directly with that server address, something along these lines:

urllib2.quote(idExtractedFromJson.encode("utf-8")

[*] Thanks @jDo for pointing that out, I initially guessed the wrong codepage.

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