相关疑难解决方法(0)

访问JSON元素

我从URL获取天气信息.

weather = urllib2.urlopen('url')
wjson = weather.read()
Run Code Online (Sandbox Code Playgroud)

而我得到的是:

{
  "data": {
     "current_condition": [{
        "cloudcover": "0",
        "humidity": "54",
        "observation_time": "08:49 AM",
        "precipMM": "0.0",
        "pressure": "1025",
        "temp_C": "10",
        "temp_F": "50",
        "visibility": "10",
        "weatherCode": "113",
        "weatherDesc": [{
            "value": "Sunny"
        }],
        "weatherIconUrl": [{
            "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
        }],
        "winddir16Point": "E",
        "winddirDegree": "100",
        "windspeedKmph": "22",
        "windspeedMiles": "14"
    }]        
 }
}
Run Code Online (Sandbox Code Playgroud)

如何访问我想要的任何元素?

如果我这样做:print wjson['data']['current_condition']['temp_C']我收到错误说:

字符串索引必须是整数,而不是str.

python json

65
推荐指数
3
解决办法
16万
查看次数

Python访问嵌套的JSON数据

我正在尝试使用zippopotam.us获取特定城市的邮政编码.我有以下代码可用,除非我尝试访问post code返回的键TypeError: expected string or buffer

r = requests.get('http://api.zippopotam.us/us/ma/belmont')
j = r.json()

data = json.loads(j)

print j['state']
print data['places']['latitude']
Run Code Online (Sandbox Code Playgroud)

完整的JSON输出:

{
"country abbreviation": "US",
"places": [
    {
        "place name": "Belmont",
        "longitude": "-71.4594",
        "post code": "02178",
        "latitude": "42.4464"
    },
    {
        "place name": "Belmont",
        "longitude": "-71.2044",
        "post code": "02478",
        "latitude": "42.4128"
    }
],
"country": "United States",
"place name": "Belmont",
"state": "Massachusetts",
"state abbreviation": "MA"
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

python rest json

45
推荐指数
4
解决办法
14万
查看次数

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

首先,我会自由地承认,只不过是一个笨拙的文科家伙,他在这个脚本编写的东西中完全是自学成才.也就是说,我试图使用以下代码从USGS水资源服务获取价值:

def main(gaugeId):

    # import modules
    import urllib2, json

    # create string
    url = "http://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]
Run Code Online (Sandbox Code Playgroud)

虽然我找到了一些关于如何从JSON响应中提取所需值的教程,但大多数都非常简单.我遇到的困难是从这个服务正在返回的非常复杂的响应中提取.通过回答,我可以看到我想要的是两个不同部分的值和时间值.因此,我可以看看响应,看看我需要什么,我不能,为了我的生活,弄清楚如何提取这些值.

python json

19
推荐指数
4
解决办法
12万
查看次数

使用Python从JSON API中提取数据

我通过这一部分:

如何从该URL中提取数据?我只想打印出来"networkdiff": 58954.60268219.

from urllib import urlopen

url = urlopen('http://21.luckyminers.com/index.php?page=api&action=getpoolstatus&api_key=8dba7050f9fea1e6a554bbcf4c3de5096795b253b45525c53562b72938771c41').read()

print url
Run Code Online (Sandbox Code Playgroud)

这是打印url命令导致API显示的内容:

{
    "getpoolstatus": {
        "version": "1.0.0",
        "runtime": 16.618967056274,
        "data": {
            "pool_name": "21 Coin Pool @ Luckyminers.com",
            "hashrate": 485426748,
            "efficiency": 98.1,
            "workers": 14,
            "currentnetworkblock": 12025,
            "nextnetworkblock": 12026,
            "lastblock": 12023,
            "networkdiff": 58954.60268219,
            "esttime": 521.61956775542,
            "estshares": 241478052.58625,
            "timesincelast": 427,
            "nethashrate": 485426748
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

python api json openurl

8
推荐指数
2
解决办法
8万
查看次数

如何从Python中的JSON对象中提取数据?

我试图从api调用api.trends()[Tweepy]返回的JSON对象中提取数据,但我无法提取数据.

任何人都可以给我一个如何从JSON对象中提取数据的示例.我想以表格形式提取数据.

提前致谢.

python json simplejson tweepy

5
推荐指数
1
解决办法
2万
查看次数

使用python从JSON文件中提取部分数据

我一直在尝试从JSON文件中仅提取某些数据.我设法解码JSON并将想要的数据放入python dict中.当我打印出dict时,它会显示所有想要的数据,但是当我尝试将dict写入新文件时,只会写入最后一个对象.我无法理解的一件事也是为什么当我打印dict时,我会得到多个dicts对象而不是我期望的对象.

我的代码:

import json
input_file=open('json.json', 'r')
output_file=open('test.json', 'w')
json_decode=json.load(input_file)
for item in json_decode:
    my_dict={}
    my_dict['title']=item.get('labels').get('en').get('value')
    my_dict['description']=item.get('descriptions').get('en').get('value')
    my_dict['id']=item.get('id')
    print my_dict
back_json=json.dumps(my_dict, output_file)
output_file.write(back_json)
output_file.close() 
Run Code Online (Sandbox Code Playgroud)

我的json.json文件:

[
{"type":"item","labels":{"en":{"language":"en","value":"George Washington"}},"descriptions":{"en":{"language":"en","value":"American politician, 1st president of the United States (in office from 1789 to 1797)"}},"id":"Q23"},
{"type":"item","aliases":{"en":[{"language":"en","value":"Douglas Noël Adams"},{"language":"en","value":"Douglas Noel Adams"}]},"labels":{"en":{"language":"en","value":"Douglas Adams"}},"descriptions":{"en":{"language":"en","value":"English writer and humorist"}},"id":"Q42"},
{"type":"item","aliases":{"en":[{"language":"en","value":"George Bush"},{"language":"en","value":"George Walker Bush"}]},"labels":{"en":{"language":"en","value":"George W. Bush"}},"descriptions":{"en":{"language":"en","value":"American politician, 43rd president of the United States from 2001 to 2009"}},"id":"Q207"},
{"type":"item","aliases":{"en":[{"language":"en","value":"Velázquez"},{"language":"en","value":"Diego Rodríguez de Silva y Velázquez"}]},"labels":{"en":{"language":"en","value":"Diego Velázquez"}},"descriptions":{"en":{"language":"en","value":"Spanish painter who was the leading artist …
Run Code Online (Sandbox Code Playgroud)

python json dictionary decode extract

5
推荐指数
1
解决办法
6万
查看次数

如何键入深度嵌套的字典和列表结构

在给定的

d = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
Run Code Online (Sandbox Code Playgroud)

问题要求我打印'me'

我试图理解字典中给定的键和值来查找基于该键的任何关系,但无法这样做。

在进一步潜水之前是否应该了解某些功能?

python dictionary

3
推荐指数
1
解决办法
237
查看次数

如何在python中选择特定的json元素

我无法弄清楚如何选择JSON对象中的特定元素,我无法想出谷歌的搜索短语.

这是我的JSON

{
  "originalRequest": {
    "category": {}
  },
  "totalResultSize": 209,
  "products": [
    {
      "id": "1000004006560322",
      "ean": "0828768235928",
      "gpc": "music",
      "title": "title",
      "specsTag": "tag",
      "summary": "summary",
      "rating": 45,
      "urls": [
        {
          "key": "DESKTOP",
          "value": "http://www.url.com"
        },
        {
          "key": "MOBILE",
          "value": "https://m.url.com"
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

如何选择密钥为MOBILE的URL?

谢谢!

python json

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×8

json ×7

dictionary ×2

api ×1

decode ×1

extract ×1

openurl ×1

rest ×1

simplejson ×1

tweepy ×1