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

编程入门 行业动态 更新时间:2024-10-14 12:24:44
本文介绍了从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("spatialreference/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("spatialreference/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字符串中删除反斜杠?

本文发布于:2023-11-06 08:14:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   字符串   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!