如何在Javascript中打印json的特定部分?

我有一个 json,可在https://imgur.com/a/F3kV29F 或此处查看https://dweet.io/get/dweets/for/shyam__5

在 python 中,我可以通过执行以下操作打印 yearlyWatts:

print(collection[1]['content']['yearlyWatts'])

其中collection是json,由:

collection = (dweepy.get_dweets_for('shyam__5'))

我正在尝试在 Javascript 中做同样的事情。目前,我已经做了:

getCryptoCurrencyInfo(5)
    .then(currencyInfo => { 
           console.log(currencyInfo[1].yearlyWatts)

这不起作用,我没有输出。

请不要关注函数 getCryptoCurrencyInfo,如果有人能告诉我在 console.log(HERE) 中写什么以输出 111255.51 的年瓦数,我将不胜感激

任何帮助,将不胜感激。谢谢!


胡说叔叔
浏览 157回答 3
3回答

四季花海

假设您想要一个年瓦特。const data = {    "this": "succeeded",    "by": "getting",    "the": "dweets",    "with": [{            "thing": "shyam__5",            "created": "2020-07-03T08:38:01.184Z",            "content": {                "test": "test"            }        },        {            "thing": "shyam__5",            "created": "2020-07-03T08:37:58.068Z",            "content": {                "yearlyWatts": 111429.4            }        }    ]}console.log(data.with[1].content.yearlyWatts)

qq_遁去的一_1

在后端模块中,代码为:import { fetch } from 'wix-fetch'export function getCryptoCurrencyInfo() {  const url = 'https://dweet.io/get/dweets/for/shyam__5'  console.log(url)  return fetch(url, { method: 'get' }).then(response => response.json())}为了让它工作,网站页面(前端)这样说:// For full API documentation, including code examples, visit https://wix.to/94BuAAsimport { getCryptoCurrencyInfo } from 'backend/serviceModule'import { fetch } from 'wix-fetch'$w.onReady(function() {  //TODO: write your page related code here...  getCryptoCurrencyInfo().then(currencyInfo => {    const data = currencyInfo    console.log(data.with[1].content.yearlyWatts)    console.log(data.with[2].content.monthlyWatts)    console.log(data.with[3].content.currentDailyCarbonSaved)    console.log(data.with[4].content.currentDailyWatts)  })})

慕桂英546537

考虑到global_obj你的json_object,你可以这样做global_obj.with.find(element => element.thing==="shyam__5");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript