如何使用Python访问Json的特定部分

2024-04-29 07:07:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图访问下面json示例的nodes部分。你知道吗

{
"pages": {
"prev": "",
"next": "/product_feed/threads/v2?filter=marketplace%28US%29&filter=language%28en%29&filter=channelId%28010794e5-35fe-4e32-aaff-cd2c74f89d61%29&anchor=50",
"totalPages": 48,
"totalResources": 2385
  },

"objects": [
{
  "id": "146e9350-97d9-4dd4-ac3a-c1d85fe49021",
  "channelId": "010794e5-35fe-4e32-aaff-cd2c74f89d61",
  "channelName": "SNKRS WEB",
  "marketplace": "US",
  "language": "en",
  "lastFetchTime": "2018-08-01T22:53:04.874Z",
  "publishedContent": {
    "marketplace": "US",
    "collectionGroupId": "010794e5-35fe-4e32-aaff-cd2c74f89d61",
    "createdDateTime": "2018-08-01T22:52:34.423Z",
    "language": "en",
    "viewStartDate": "2018-08-01T22:50:00.000Z",
    "type": "thread",
    "version": "1533163903286",
    "classifications": [

    ],
    "nodes": [
      {
        "subType": "image",
        "id": "a06c8da1-e700-45fa-8bb4-9b793dc34f9f",
        "type": "card",
        "version": "1533163901597",
        "properties": {
          "portraitId": "hw6nubnjepxt1gevqcyb",
          "squarishURL": "",
          "product": [

          ],
          "landscapeId": "xpvylaxdn1svszos2ocq",
          "altText": "Nike Moon Racer 'Black & White & Wolf Grey' Release Date",
          "portraitURL": "https://c.static-nike.com/a/images/w_1536,c_limit/hw6nubnjepxt1gevqcyb/nike-moon-racer-black-white-wolf-grey-release-date.jpg",
          "landscapeURL": "https://c.static-nike.com/a/images/w_1920,c_limit/xpvylaxdn1svszos2ocq/nike-moon-racer-black-white-wolf-grey-release-date.jpg",
          "portrait": {
            "aspectRatio": 0.66667,
            "id": "hw6nubnjepxt1gevqcyb",
            "type": "editorial",
            "url": "https://c.static-nike.com/a/images/w_1536,c_limit/hw6nubnjepxt1gevqcyb/nike-moon-racer-black-white-wolf-grey-release-date.jpg"

这是我当前的代码,我可以得到json的objects部分,但是每次我试图得到节点时都会遇到str错误。TypeError: list indices must be integers or slices, not str

import requests
import bs4 
import json
import time

endpoint = "https://api.nike.com/product_feed/threads/v2/?filter=marketplace%28US%29&filter=language%28en%29&filter=channelId%28010794e5- 
 35fe-4e32-aaff-cd2c74f89d61%29"
req = requests.get(endpoint)
reqJson = json.loads(req.text)
for object in reqJson['objects']['nodes']:

    print (object)

Tags: httpsimportcomidjsonobjectstypestatic