python的jsonpath_Python,jsonpath:如何使用jsonpath正确解析?

我有一个实施问题。。。#!/usr/bin/python

#This is the API for BTC price request.

# Average all the amounts, and push that to the program

import json

import urllib.request

from jsonpath_rw import parse as parse_jsonpath

class BtcAPI:

def __init__(self, url, api_id, json_key):

self.url = url

self.api_id = api_id

self.json_key = json_key

def btc_api_call(self):

hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }

req = urllib.request.Request(self.url, headers=hdr)

readdata = urllib.request.urlopen(req)

json_data = readdata.read()

json_dict = json.loads(json_data)

results = parse_jsonpath(self.json_key).find(json_dict)

print(results)

class Price:

def __init__(self, api_id, url, json_key):

self.api_id = api_id

self.url = url

self.json_key = json_key

def pass_for_request(self):

get_price = BtcAPI(self.url, self.api_id, self.json_key)

get_price.btc_api_call()

def Coindesk():

coindesk = Price("coindesk","https://api.coindesk.com/v1/bpi/currentprice.json","time.updated")

coindesk.pass_for_request()

为“json_key”传递的值是“bpi.USD.rate_float”。。。在这个网址里面。它被传递给一个名为“Price”的类,该类创建传递给上述代码所包含的类的变量。coindesk = Price("coindesk","api.coindesk.com/v1/bpi/currentprice.json", "bpi.USD.rate_float")

这是我的目标json。。。正在尝试获取速率浮动键:{

"time": {

"updated": "Feb 5, 2018 18:34:00 UTC",

"updatedISO": "2018-02-05T18:34:00+00:00",

"updateduk": "Feb 5, 2018 at 18:34 GMT"

},

"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",

"chartName": "Bitcoin",

"bpi": {

"USD": {

"code": "USD",

"symbol": "$",

"rate": "7,004.9588",

"description": "United States Dollar",

"rate_float": 7004.9588

}

}

当我运行这个程序时,它会使用“bpi.USD.rate戥u float”通过jsonpath向我发送整个json文件的输出,而不是我试图通过jsonpath指向的特定键

我在用jsonpath。

如何使用jsonpath有效地将速率浮动键作为目标?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值