如何使用Python 2.7将JSON字符串保存为新的.json文件

时间:2018-02-28 00:10:07

标签: python json

我有一个Json字符串:

{"spatialReference":{"wkid":102100},"geometryType":"esriGeometryPolygon","features":[{"geometry":{"rings":[[[-13945398.575078921,7313414.186040432],[-13945392.60343609,7313379.550512001],[-13945410.518364588,7313360.142672794],[-13945433.509189496,7313387.313647684],[-13945398.575078921,7313414.186040432]]],"spatialReference":{"wkid":102100}}},{"geometry":{"rings":[[[-13945451.424117994,7313427.025072522],[-13945426.641800238,7313432.399551072],[-13945432.314860929,7313413.588876149],[-13945449.035460861,7313412.991711865],[-13945451.424117994,7313427.025072522]]],"spatialReference":{"wkid":102100}}}]}

如何使用python将此字符串保存为新的.json文件?我可能需要创建一个新的空.json文件,然后用字符串填充它。

谢谢!

2 个答案:

答案 0 :(得分:0)

如果它已经是一个字符串,只需执行:

file = open('json.json','w')
file.write('your_json_string')
file.close()

答案 1 :(得分:0)

这是你如何做到的。但这是一个骗局......

import json

with open('output.json', 'w') as f:
    json.dump({'a':'b'},f,indent=2)  # {'a... being your json string