简体   繁体   English

访问嵌套的JSON对象

[英]Accessing nested JSON objects

I'm building an Angular App and also using angular translate as it needs to be in dual languages. 我正在构建一个Angular应用程序,并且还需要使用角度翻译,因为它需要使用两种语言。

I've seem to have created my JSON properly (ran it through a checker), but when I try to access items within the JSON object beyond the first level, it returns undefined. 似乎已经正确创建了JSON(通过检查器运行了它),但是当我尝试访问JSON对象中超出第一级的项目时,它返回未定义。

For instance, my JSON within the angular translations is this: 例如,我在角度转换中的JSON是这样的:

$translateProvider.translations('en', {
 "SEARCH": {
    "SEARCH" : "Recherce",
    "ABILITY" : "Abilities",
    "MANAGEMENT" : "Management Competencies",
    "PERSONAL" : "Personal Suitability"
 },

 "ABILITIES": {
    "TITLE" : "Test Title here",
    "ADVISORY": {
      "TITLE" : "Advisory Skills",
      "QUESTIONS": [
        {
          "TYPE" : "A",
          "LEVEL" : "45",
          "DESCRIPTION" : "Can you tell me how awesome you are"
        },
        {
          "TYPE" : "B",
          "LEVEL" : "100",
          "DESCRIPTION" : "Tell me about your wicked project"
        }
      ]
    }
  },

  "HEADLINE": "Oh No!",
  "SUB_HEADLINE": "Looks like you are not amazing"
});   

And to begin accessing the data in the JSON object, I do 为了开始访问JSON对象中的数据,我做了

list = $translateProvider.translations('en');

Now, when outputting items in the console to see if they work I do this: 现在,在控制台中输出项目以查看它们是否起作用时,请执行以下操作:

console.log(list);
var getTitle = list.HEADLINE;
var getSearch = list.SEARCH.ABILITY;
console.log(getSearch);
console.log(getTitle);

This is where it gets odd. 这就是奇怪的地方。

The 'list' returns the JSON array I specified “列表”返回我指定的JSON数组

Getting the HEADLINE returns Oh No! 得到HEADLINE返回Oh No!

But getting list.SEARCH.ABILITY returns an undefined 但是获取list.SEARCH.ABILITY返回一个undefined

What gives!? 是什么赋予了!? I haven't event tried to access the stuff I really want in the really nested array "ABILITIES" 我还没有尝试访问真正嵌套数组“ ABILITIES”中我真正想要的东西

Keep in mind that Angular Translate uses the format {{ 'ABILITIES.ADVISORY.TITLE' | translate }} 请记住,Angular Translate使用以下格式: {{ 'ABILITIES.ADVISORY.TITLE' | translate }} {{ 'ABILITIES.ADVISORY.TITLE' | translate }} to output the JSON onto the HTML page {{ 'ABILITIES.ADVISORY.TITLE' | translate }}以将JSON输出到HTML页面

尝试list.SEARCH["ABILITY"].value

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

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