首页 > 解决方案 > 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'

标签: pythonjsonencodingutf-8foursquare

解决方案


尝试了解查询的真实类型,因为可能不是字典,具有以下内容:

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}'

推荐阅读