简体   繁体   English

解析 JSON 文件时,我可以用变量零替换 Javascript 中的 int

[英]Can i replace the int in my Javascript with the Variable Zero when parsing a JSON file

here is the JSON file I would like to parse这是我想解析的 JSON 文件

{
  "items": [
    0 {
      "snippet": {
        "liveBroadcastContent": "live",
      }
    }
  ]
}

I am using this to parse it in Javascript:我在 Javascript 中使用它来解析它:

var zero;
zero = 0;
isthisThingLive = data.items.zero.snippet.liveBroadCastContent;
console.log(isthisThingLive);

(I am not including the server code to avoid your boredom, I know it works because I use it in multiple other places) (我不包括服务器代码以避免您的无聊,我知道它有效,因为我在其他多个地方使用它)

I run into an error whenever I replace the "Zero" variable with the 0 Int.每当我用 0 Int 替换“零”变量时都会遇到错误。 Is there a reason for this?是否有一个原因? I am relatively new to using server requests and parsing and any help would be appreciated!我对使用服务器请求和解析比较陌生,任何帮助将不胜感激!

The "Data" variable is used to reach the server, this is not the issue, the error I'm getting is "You can not get "Snippet" of undefined, meaning that the Zero variable is failing to be recognized but the server! “数据”变量用于到达服务器,这不是问题,我得到的错误是“您无法获得未定义的“片段”,这意味着零变量无法识别但服务器!

Thanks for your help!谢谢你的帮助!

This is the proper json:这是正确的 json:

{
  "items": [
    {
      "snippet": {
        "liveBroadcastContent": "live"
      }
    }
  ]
}

This is how you can access liveBroadCastContent这是您可以访问 liveBroadCastContent 的方式

const isthisThingLive = data.items[0].snippet.liveBroadCastContent;
console.log(isthisThingLive);

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

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