如何从JSON响应中提取单个值?

编程入门 行业动态 更新时间:2024-10-06 18:22:00
本文介绍了如何从JSON响应中提取单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先,我将自由地允许自己成为一个笨拙的文科专家,他完全可以自学此脚本.就是说,我正在尝试使用以下代码从USGS水数据服务获取值:

First off, I will freely concede to being little more than a clumsy liberal arts guy who is completely self taught in this scripting thing. That said, I am attempting to get values from a the USGS Water Data Service using the code below:

def main(gaugeId): # import modules import urllib2, json # create string url = "waterservices.usgs.gov/nwis/iv/?format=json&sites=" + gaugeId + "&parameterCd=00060,00065" # open connection to url urlFile = urllib2.urlopen(url) # load into local JSON list jsonList = json.load(urlFile) # extract and return # how to get cfs, ft, and zulu time? return [cfs, ft, time]

尽管我找到了一些有关如何从JSON响应中提取所需值的教程,但大多数教程都非常简单.我遇到的困难是从该服务返回的看起来非常复杂的响应中提取出来的.查看响应,可以看到我想要的是来自两个不同部分的值和一个时间值.因此,我可以查看响应并查看我的需求,但我一生都无法弄清楚如何提取这些值.

Although I have found some tutorials regarding how to extract the desired values from a JSON response, most are fairly simple. The difficulty I am having is extracting from what looks like a very complicated response this service is returning. Looking through the response, I can see what I want is the value from two different sections and a time value. Hence, I can look at the response and see what I need, I just cannot, for the life of me, figure out how to get these values extracted.

推荐答案

使用json.loads会将您的数据转换为python 词典.

using json.loads will turn your data into a python dictionary.

使用['key']

resp_str = { "name" : "ns1:timeSeriesResponseType", "declaredType" : "org.cuahsi.waterml.TimeSeriesResponseType", "scope" : "javax.xml.bind.JAXBElement$GlobalScope", "value" : { "queryInfo" : { "creationTime" : 1349724919000, "queryURL" : "waterservices.usgs.gov/nwis/iv/", "criteria" : { "locationParam" : "[ALL:103232434]", "variableParam" : "[00060, 00065]" }, "note" : [ { "value" : "[ALL:103232434]", "title" : "filter:sites" }, { "value" : "[mode=LATEST, modifiedSince=null]", "title" : "filter:timeRange" }, { "value" : "sdas01", "title" : "server" } ] } }, "nil" : false, "globalScope" : true, "typeSubstituted" : false }

将翻译成python字典

would translate into a python diction

resp_dict = json.loads(resp_str) resp_dict['name'] # "ns1:timeSeriesResponseType" resp_dict['value']['queryInfo']['creationTime'] # 1349724919000

更多推荐

如何从JSON响应中提取单个值?

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

发布评论

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

>www.elefans.com

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