【问题标题】:get values from JSON array in string and put in variables从字符串中的 JSON 数组中获取值并放入变量中
【发布时间】:2022-02-04 19:42:39
【问题描述】:

我再次在这里向您寻求帮助。我有这个 JSON 数组字符串,我想获取这些值并放入一个变量中:

“名称”:“波尔图” “纬度”:41.1494512 "lon":-8.6107884

'[{"name":"Porto","local_names":{"ascii":"Porto","el":"Πόρτο","kn":"ಪೋರ್ಟೊ","es":"Oporto","hu":"Porto","pt":"Porto","uk":"Порту","ar":"بورتو","ru":"Порту","feature_name":"Porto","lt":"Portas","sr":"Порто"},"lat":41.1494512,"lon":-8.6107884,"country":"PT"}]'

【问题讨论】:

  • 请告诉我们你做了什么!
  • 我在这里: const getData = JSON.parse(citySearch) const [{ name }] = getData;常量 [{ 纬度 }] = getData;常量 [{ lon }] = getData; console.log([{ name }]) console.log([{ lat }]) console.log([{ lon }]) 返回:[ { name: 'Porto' } ] [ { lat: 41.1494512 } ] [ { lon: -8.6107884 } ]
  • 然后我这样做了: const city = name const lati = lat const long = lon console.log(city) console.log(lat) console.log(lon) 返回我需要的东西 Porto 41.1494512 - 8.6107884 但我想在循环中这样做

标签: javascript arrays json object getjson


【解决方案1】:

首先,谷歌一下。

其次,拿这个: 看起来 JSON 已被字符串化。如果有,请确保首先使用 JSON.parse。 然后,你可以简单地做这样的事情

info={"name":array[0].name,"lat":array[0].lat,"lon":array[0].Lon}

我建议继续阅读 https://www.w3schools.com/js/js_arrays.asphttps://www.w3schools.com/js/js_objects.asp

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多