簡體   English   中英

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

[英]How to print specific part of json in Javascript?

我有一個 json,可在https://imgur.com/a/F3kV29F或此處查看https://dweet.io/5/dweet

在 python 中,我可以通過以下方式打印年瓦:

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

其中 collection 是 json,由以下人員完成:

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

我試圖在 Javascript 中做同樣的事情。 目前,我已經做了:

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

這不起作用,我沒有得到 output。

請不要關注 function getCryptoCurrencyInfo,如果有人能告訴我在 console.log(HERE) 中寫什么以便 output 年瓦數為 111255.51,我將不勝感激

任何幫助,將不勝感激。 謝謝!

假設您想要一個年瓦特。

 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)

感謝 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)
  })
})

考慮global_obj你的json_object ,你可以這樣做

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM