1

我有一个 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 的年瓦数,我将不胜感激

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

4

3 回答 3

1

假设您想要一个年瓦特。

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)

于 2020-07-03T08:58:34.087 回答
1

感谢 xMayank 的帮助,我想出了如何做到这一点。在后端模块中,代码为:

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)
  })
})
于 2020-07-03T09:27:18.243 回答
0

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

global_obj.with.find(element => element.thing==="shyam__5");
于 2020-07-03T09:00:35.503 回答