2012-03-20 60 views
-4

我在Windows上運行python並試圖漂亮地打印json輸出。這裏是我得到的:python:如何在Windows上使用json.dumps?

>>> ta 
{u'Status': {u'code': 200, u'request': u'geocode'}, u'Placemark': [{u'Point': {u'coordinates': [34.777821, 32.066157, 0]}, u'ExtendedData': {u'LatLonBox': {u'west': 34.71 
37913, u'east': 34.8418507, u'north': 32.1039719, u'south': 32.0283265}}, u'AddressDetails': {u'Country': {u'CountryName': u'\u05d9\u05e9\u05e8\u05d0\u05dc', u'Locality': 
{u'LocalityName': u'\u05ea\u05dc \u05d0\u05d1\u05d9\u05d1 \u05d9\u05e4\u05d5'}, u'CountryNameCode': u'IL'}, u'Accuracy': 4}, u'id': u'p1', u'address': u'Tel Aviv, Israel 
'}], u'name': u'Tel Aviv'} 
>>> json.dumps(ta, sort_keys=True, indent = 4) 
'{\n "Placemark": [\n  {\n   "AddressDetails": {\n    "Accuracy": 4, \n    "Country": {\n     "CountryName": "\\u 
05d9\\u05e9\\u05e8\\u05d0\\u05dc", \n     "CountryNameCode": "IL", \n     "Locality": {\n      "LocalityName": "\\u05ea\\u 
05dc \\u05d0\\u05d1\\u05d9\\u05d1 \\u05d9\\u05e4\\u05d5"\n     }\n    }\n   }, \n   "ExtendedData": {\n    "LatLo 
nBox": {\n     "east": 34.8418507, \n     "north": 32.1039719, \n     "south": 32.0283265, \n     "west": 34.7 
137913\n    }\n   }, \n   "Point": {\n    "coordinates": [\n     34.777821, \n     32.066157, \n 
        0\n    ]\n   }, \n   "address": "Tel Aviv, Israel", \n   "id": "p1"\n  }\n ], \n "Status": {\n 
"code": 200, \n  "request": "geocode"\n }, \n "name": "Tel Aviv"\n}' 
>>> 

它爲什麼不起作用?

+0

在什麼意義上它不工作?看起來它對我有用。 – Marcin 2012-03-20 10:33:06

回答

2

它確實有效。請記住,字典的JSON表示看起來非常像Python語法。嘗試打印出來的返回值從json.dumps,看看它看起來更像你期待:

s = json.dumps(ta, sort_keys=True, indent = 4) 
print s 
+0

愚蠢的我 - 謝謝。 – mark 2012-03-20 11:10:54

相關問題