解析api响应

问题描述 投票:0回答:1

我想解析这个api响应来获取图像网址,我发现它实际上有点令人困惑,因为我是apis的新手。

    {
      "id": "123",
      "item": [
        {
          "picture": {
            "type_id": "2",
            "url": [
              "./img.jpg"
            ],
            "is_in_description": 0,
            "gallery": {
              "url": "",
              "url_id": ""
            },
            "layout_id": "2",
            "variation_name": ""
          },
          "lister_id": "12345"
        }
      ]
    }

这是我获取api的代码,任何人都可以帮助我

fetch(url2,{
          method: 'GET'
        })
        .then((response)=> response.json())
        .then((responseJson) => {
          const newImg = responseJson.item.map( => {
            return{
              const img = 
            };
          })
          const newState = Object.assign({}, this.state, {
            items: newItems
          });

          console.log(newState);
          this.setState(newState);
        })
        .catch((error) => {
          console.log(error)
        });
reactjs rest api get fetch-api
1个回答
1
投票

使用map方法进行解析

var x =   {
      "id": "123",
      "item": [
        {
          "picture": {
            "type_id": "2",
            "url": [
              "./img.jpg"
            ],
            "is_in_description": 0,
            "gallery": {
              "url": "",
              "url_id": ""
            },
            "layout_id": "2",
            "variation_name": ""
          },
          "lister_id": "12345"
        }
      ]
    }

x.item.map(data=>{console.log(data.picture.url)}) //hope you need the url object
© www.soinside.com 2019 - 2024. All rights reserved.