python:如何在Windows上使用json.dumps?

时间:2012-03-20 10:31:33

标签: python

我在Windows上运行python并尝试打印json输出。这是我得到的:

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

为什么不起作用?

1 个答案:

答案 0 :(得分:2)

确实有效。请记住,字典的JSON表示看起来非常像Python语法。尝试打印json.dumps的返回值,看看它是否更像您期望的那样:

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