简体   繁体   English

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

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

I have a json, viewable at https://imgur.com/a/F3kV29F or here https://dweet.io/get/dweets/for/shyam__5我有一个 json,可在https://imgur.com/a/F3kV29F或此处查看https://dweet.io/5/dweet

In python, I am able to print the yearlyWatts by doing:在 python 中,我可以通过以下方式打印年瓦:

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

where collection is the json, done by:其中 collection 是 json,由以下人员完成:

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

I am trying to do the same thing in Javascript.我试图在 Javascript 中做同样的事情。 Currently, I have done:目前,我已经做了:

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

This does not work, I get no output.这不起作用,我没有得到 output。

Please do not pay attention to the function getCryptoCurrencyInfo, I would really appreciate if someone could tell me what to write in the console.log(HERE) in order to output the yearly watts of 111255.51请不要关注 function getCryptoCurrencyInfo,如果有人能告诉我在 console.log(HERE) 中写什么以便 output 年瓦数为 111255.51,我将不胜感激

Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

Suppose you want a single yearlyWatt.假设您想要一个年瓦特。

 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)

I figured out how to do it thanks to xMayank's help.感谢 xMayank 的帮助,我想出了如何做到这一点。 In the backend module, the code is:在后端模块中,代码为:

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())
}

To get it to work, the site page (front end) says this:为了让它工作,网站页面(前端)这样说:

// 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)
  })
})

considering global_obj your json_object , you can do this考虑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