这是字典还是JSON,以及如何使用python访问特定的数据点

时间:2019-02-15 21:39:15

标签: python json dictionary

网站说返回的数据为JSON格式,但是终端将代码视为字典。无论哪种情况,我都无法访问标题等数据点,我只能打印整篇文章。如何分别访问每个数据点?

{'articles': {'results': [{'uri': '1066066172', 'lang': 'eng', 'isDuplicate': True, 'date': '2019-02-15', 'time': '21:32:00', 'dateTime': '2019-02-15T21:32:00Z', 'dataType': 'news', 'sim': 0, 'url': 'https://uk.investing.com/news/stock-market-news/us-shares-higher-at-close-of-trade-dow-jones-industrial-average-up-174-1448343', 'title': 'U.S. shares higher at close of trade; Dow Jones Industrial Average up 1.74%', 'body': 'Biggest losers included Coca-Cola Company (NYSE:KO), which lost 0.76% or 0.34 points to trade at 45.24 in late trade. Apple Inc (NASDAQ:AAPL) declined 0.22% or 0.38 points to end at 170.42 and Procter & Gamble Company (NYSE:PG) gained 0.02% or 0.02 points to 98.48.\n\nAdvancing stocks outnumbered falling ones by 2320 to 682 and 109 ended unchanged; on the Nasdaq Stock Exchange, 1873 rose and 755 declined, while 97 ended unchanged on the New York Stock Exchange.\n\nThe CBOE Volatility Index, which measures the implied volatility of S&P 500 options, was down 8.08% to 14.91 a new 3-month low.\n\nIn commodities trading, Gold Futures for April delivery was up 0.82% or 10.75 to $1324.65 a troy ounce. Meanwhile, Crude oil for delivery in March rose 2.44% or 1.33 to hit $55.74 a barrel, while the April Brent oil contract rose 2.68% or 1.73 to trade at $66.30 a barrel.', 'source': {'uri': 'uk.investing.com', 'dataType': 'news', 'title': 'investing.com UK'}, 'authors': [], 'image': 'https://i-invdn-com.akamaized.net/news/LYNXNPEAAB17X_L.jpg', 'eventUri': None, 'sentiment': -0.2156862745098039, 'wgt': 287962320}], 'totalResults': 32972, 'page': 1, 'count': 1, 'pages': 32972}}

1 个答案:

答案 0 :(得分:0)

假设您的对象是一个名为obj的字典。您可以像访问Python中的任何列表/字典一样访问键:

obj["articles"]["results"][0]["uri"]

如果它是json字符串,请先执行

import json
obj = json.loads(obj)

使用type(obj)来确定它是字典还是字符串。

相关问题