从JSON字符串中删除反斜杠? [英] Remove Backslash from JSON string?

查看:1726
本文介绍了从JSON字符串中删除反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python url库从空间参考网站获取json响应.这是我的代码.我得到 response_read ="u'{\'ty​​pe \':\'EPSG \',\'properties \':{\'code \':102646}}'" ,但我需要此响应格式如下:"{'type":'EPSG','properties':{'code':102646}}".我如何以这种形式实现输出?

I am using python url library to get json response from spatial reference website.This is my code. I get response_read="u'{\'type\': \'EPSG\', \'properties\': {\'code\': 102646}}'" but i need this response in this form:"{'type': 'EPSG', 'properties': {'code': 102646}}". How i achieve output in this form?

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

推荐答案

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

I am not very sure your is a function or not. Anyways, the response you receive is having the literal character ' and you need to replace it with ".

这是工作代码:

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字符串中删除反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆