python - im working with foursquare library in python 3 and trying to convert my query which is a python dictionary into json and encode it
问题描述
im working with foursquare library in python 3 and trying to convert my query which is a python dictionary into json and encode data :
#query is a dict
query= client.venues.search(params={'categoryId':catid,'radius':radius,'ll': locus})
then:
for i in query:
jsonx=json.dumps(query[i])
pp.pprint(jsonx.encode('utf-8'))
but i cant get farsi chars and output is somthing like this:
b'[{"id": "578e4b21498e80bd308334bb", "name": "Shahid Kazemi Stadium | \\u0'
b'648\\u0631\\u0632\\u0634\\u06af\\u0627\\u0647 \\u0634\\u0647\\u06cc\\u062f'
b' \\u06a9\\u0627\\u0638\\u0645\\u06cc (\\u0648\\u0631\\u0632\\u0634\\u06af\\'
b'u0627\\u0647 \\u0634\\u0647\\u06cc\\u062f \\u06a9\\u0627\\u0638\\u0645\\u0'
b'6cc)", "location": {"address": "End of Navab Hwy.", "crossStreet": "West Sha'
b'ghayegh Blvd.", "lat": 35.61106, "lng": 51.360404, "labeledLatLngs": [{"labe'
b'l": "display", "lat": 35.61106, "lng": 51.360404}], "distance": 3391, "cc": '
b'"IR", "city": "\\u062a\\u0647\\u0631\\u0627\\u0646", "state": "\\u062a\\u06'
b'47\\u0631\\u0627\\u0646", "country": "\\u0627\\u06cc\\u0631\\u0627\\u0646", '
b'"formattedAddress": ["End of Navab Hwy. (West Shaghayegh Blvd.)", "\\u062'
b'a\\u0647\\u0631\\u0627\\u0646, \\u062a\\u0647\\u0631\\u0627\\u0646", "\\u0'
b'627\\u06cc\\u0631\\u0627\\u0646"]}, "categories": [{"id": "4bf58dd8d48988d18'
b'8941735", "name": "Soccer Stadium", "pluralName": "Soccer Stadiums", "shortN'
b'ame": "Soccer", "icon": {"prefix": "https://ss3.4sqi.net/img/categories_v2/a'
b'rts_entertainment/stadium_soccer_", "suffix": ".png"}, "primary": true}], "r'
b'eferralId": "v-1576913650", "hasPerk": false}, {"id": "5722fdc2498ee138baea8'
解决方案
尝试了解查询的真实类型,因为可能不是字典,具有以下内容:
print(type(query))
我不明白 FOR 循环的原因。
jsonx=json.dumps(query)
pp.pprint(jsonx)
我用普通字典模拟,似乎可以正常工作:
import json
import pprint as pp
query = {"key1" : 1, "key2" : 2}
jsonx=json.dumps(query, ensure_ascii=False)
pp.pprint(jsonx)
输出:
'{"key1": 1, "key2": 2}'
推荐阅读
- node.js - Socket.io 客户端关闭,原因是 ping 超时
- python - 如何检查一个目录是否有python中的音乐文件?
- powershell - PowerScript:System.Windows.Forms.FolderBrowserDialog 在后台打开
- javascript - 带有 Javascript 功能的引导轮播播放和暂停 (JQUERY)
- javascript - 限制文本输入中的字符数
- angular - 如何按角度值对字典进行排序
- go - 如何将 Sentry 与 go.uber.org/zap/zapcore 记录器一起使用
- python - 为什么在我的 Python 代码 Cows and Bulls 中出现“IndexError: list index out of range”?
- python - 用 pyaudio 播放的声音似乎正确但太短了
- php - 如何有条件地将同一网址上的其他网站发送给用户?