为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

胡说叔叔 2022-10-27 16:03:10
我有一个 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 的年瓦数,我将不胜感激任何帮助,将不胜感激。谢谢!
查看完整描述

3 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

假设您想要一个年瓦特。


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)


查看完整回答
反对 回复 2022-10-27
?
qq_遁去的一_1

TA贡献1725条经验 获得超8个赞

在后端模块中,代码为:


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/94BuAAs


import { 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)

  })

})


查看完整回答
反对 回复 2022-10-27
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

考虑到global_obj你的json_object,你可以这样做

global_obj.with.find(element => element.thing==="shyam__5");


查看完整回答
反对 回复 2022-10-27
  • 3 回答
  • 0 关注
  • 156 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号