从 JSON 字符串中删除反斜杠?

Remove Backslash from JSON string?

我正在使用 python url 库从空间参考中获取 json 响应 website.This 是我的代码。我得到 response_read="u'{\'type\': \'EPSG\', \'properties\': {\'code\': 102646}}'" 但我需要这种形式的回复:"{'type': 'EPSG' , 'properties': {'code': 102646}}"。我如何以这种形式实现输出?

headers = {'User-Agent': 'Mozilla/5.0'}
 req = urllib2.Request("http://spatialreference.org/ref/esri/"nad-1983-stateplane-california-vi-fips-0406-feet"/json/", None, headers)
        response = urllib2.urlopen(req)
        response_read = response.read().decode('utf-8')
        result = json.dumps(response_read)  
        epsg_json = json.loads(result)
        epsg_code = epsg_json['properties']['code']
        return epsg_code

我不太确定你是不是一个函数。无论如何,您收到的响应是文字字符 ',您需要将其替换为 ".

这是工作代码:

import urllib2,json
headers = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request("http://spatialreference.org/ref/esri/nad-1983-stateplane-california-vi-fips-0406-feet/json/", None, headers)
response = urllib2.urlopen(req)
response_read = response.read()
epsg_json = json.loads(response_read.replace("\'", '"'))
epsg_code = epsg_json['properties']['code']
print(epsg_code)

希望对您有所帮助。

您需要先使用转储然后加载

json_data = json.dumps(response_read)
json_without_slash = json.loads(json_data)