【问题标题】:react-native Json copy array urlreact-native Json 复制数组 url
【发布时间】:2019-12-17 22:31:43
【问题描述】:

我正在尝试通过 json url 复制 types[]。

我的网址打开这个:

{
  "html_attributions": [],
  "results": [
    {
      "geometry": {
        "location": {
          "lat": -27.00170259999999,
          "lng": -48.62017489999999
        },
        "viewport": {
          "northeast": {
            "lat": -27.00031801970849,
            "lng": -48.6187919197085
          },
          "southwest": {
            "lat": -27.0030159802915,
            "lng": -48.6214898802915
          }
        }
      },
      "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
      "id": "96213cc395d0c8ccaa3176875ec8aa2b89f852f8",
      "name": "Dois Amores Comida Árabe",
      "place_id": "ChIJ_RggImq22JQRqQgcG98nfn4",
      "plus_code": {
        "compound_code": "X9XH+8W Balneário Camboriú, SC, Brasil",
        "global_code": "584HX9XH+8W"
      },
      "reference": "ChIJ_RggImq22JQRqQgcG98nfn4",
      "scope": "GOOGLE",
=---> "types": [
        "restaurant",
        "food",
        "point_of_interest",
        "establishment"
      ],
      "vicinity": "Avenida Atlântica, 4054 - Centro, Balneário Camboriú"
    }
  ],
  "status": "OK"
}

我想将 types [] 列表返回到列表,以便我知道该位置适合什么类型。 我没有找到任何方法,我得到了更接近的东西。

和我的代码:

const url = 'myurlgoogleapi';
fetch(url, {
    method: 'POST',
    headers: {
      Accept: 'application/json',

      'Content-Type': 'application/json',
    },
    body: JSON.stringify({

      "html_attributions": [],
      "results": [{
        "types" ["name"]
      }]

    }),
  }).then((response) => response.json())
  .then((responseJson) => {
    console.log(responseJson[0].name);
  })
  .catch((error) => {
    console.error(error);
  });

我使用 console.log 只是为了检查是否可以工作,但只是错误。 我需要具体的 type[] 来返回你的值。

【问题讨论】:

  • 错误是什么? console.log(responseJson) 给出了什么?
  • 调试所有 json 正文,但不是 types[] 特定的

标签: javascript json react-native url


【解决方案1】:

如果你有超过3种类型,你的条件就会失败,更好,你遵循这个:

return fetch(url)
            .then((response) => response.json())
            .then((responseJson) => {

          const tipe = responseJson.results[0].types.map((type) => type) ;

           this.setState({tipe});



            })
            .catch((error) => {
                console.error(error);
            });

【讨论】:

    【解决方案2】:

    我找到了解决办法:

    return fetch(url)
                .then((response) => response.json())
                .then((responseJson) => {
                    const tipe = (responseJson.results[0].types[0] + " " + responseJson.results[0].types[1] + " " + responseJson.results[0].types[2]);
                    this.setState({ tipe });
                })
                .catch((error) => {
                    console.error(error);
                });
    
    

    首先我只需要声明 results[] 数组,然后声明 types[] = responseJson.results[0].types[0]

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 2020-03-09
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多