[英]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. 嗨,我正在研究一个简单的程序,该程序从json文件中获取数据(通过带有处理数据的flask的html表单输入),并使用此数据进行API调用。
So I have some JSON like this: 所以我有一些像这样的JSON:
[{"id": "ßLÙ", "server": "NA"}]
and I want to send the id to an api call like this example: 我想将ID发送到api调用,例如以下示例:
http://apicallnamewhatever+id=ßLÙ
however when i load the json file into my app.py
with the following command 但是当我使用以下命令将json文件加载到我的
app.py
时
ids = json.load(open('../names.json'))
json.load seems to alter the id from 'ßLÙ'
to 'ßLÙ'
json.load似乎将ID从
'ßLÙ'
更改为'ß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Ù'
我不确定为什么会在json.load期间发生这种情况,但是我需要找到一种方法来将
'ßLÙ'
放入api调用中,而不是变形的'ß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. 看来您的
names.json
是用"utf-8"
编码的,但是您将其打开为"windows-1252"
[*]或类似的名称。 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: 而且您可能还应该对
id
进行URL编码,而不是直接将其与该服务器地址连接起来,如下所示:
urllib2.quote(idExtractedFromJson.encode("utf-8")
[*] Thanks @jDo for pointing that out, I initially guessed the wrong codepage. [*]感谢@jDo指出这一点,我最初猜错了代码页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.