简体   繁体   English

如何从 API 访问 Json 嵌套对象中的数据

[英]How to access data inside Json nested objects from an API

I am using cryptocomare API to get crypto coins data within a Nextjs App.我正在使用 cryptocomare API 在 Nextjs 应用程序中获取加密硬币数据。 What i doing is that when a user clicks on a perticular symbol, i redirect it to the coin details page where i try to extract the clicked symbol with getServerSideProps as follows and then dynamically put in the API call and send it to the API server.我所做的是,当用户单击特定符号时,我将其重定向到硬币详细信息页面,在该页面中我尝试使用 getServerSideProps 提取单击的符号,如下所示,然后动态放入 API 调用并将其发送到 API 服务器。

` `

export const getServerSideProps = async (context) => {
  const res = await fetch(
    `https://min-api.cryptocompare.com/data/pricemultifull?tsyms=USD&fsyms=${context.params.symbol}`
  );  
  const icon = await res.json();
  return {
    props: {
      icon,
    },
  };
};

` This call returns a json object of nested objects and it goes to 2-3 levels deep. ` 此调用返回 json object 的嵌套对象,它深入 2-3 层。 On Top it looks like following: API call response在顶部,它看起来如下所示: API 呼叫响应

Inside my code, I want to access the data Object -> RAW -> (whatever the user clicked on).在我的代码中,我想访问数据 Object -> RAW ->(无论用户点击什么)。 But, Since the Symbol or coin queried by the user is dynamic (means i can't predict what is clicked) I never know what to query.但是,由于用户查询的符号或硬币是动态的(意味着我无法预测点击了什么)我永远不知道要查询什么。 SO i tried this to access the data object.RAW[0]所以我试过这个来访问数据object.RAW[0]

In principal it should give me the whatever object is inside the object.RAW But it returns undefined原则上它应该给我 object 里面的object.RAW但它返回undefined

Can please someone guide me, how can i get the data inside object.RAW without knowing what is inside?请有人指导我,我如何在不知道里面是什么的情况下获取object.RAW中的数据? Thanks!谢谢!

I have tried object.RAW[0] to access the data...,....我试过object.RAW[0]来访问数据......,......

You can use Object.values(object.RAW) to get an array of the values inside RAW (assuming RAW is not undefined)您可以使用Object.values(object.RAW)获取 RAW 中的值数组(假设RAW未定义)

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM