如何通过Javascript动态访问JSON元素?

时间:2011-06-30 06:34:40

标签: javascript jquery json

我的Json文件是:

    {
  "root": {
    "Quizsize": null,
    "{urn:abc.com/xmlns/mona/page}page": [
      {"{urn:abc.com/xmlns/mona/page}nut": {
              "{urn:abc.com/xmlns/mona/page}uttering": {
                "{urn:abc.com/xmlns/mona/utterance}info": null,
                "{urn:abc.com/xmlns/mona/utterance}utterance": {
                  "@lang": "fr",
                  "{urn:abc.com/xmlns/mona/utterance}ezread": {
                    "@text": "",
                    "@lang": "fr",
                    "@kind": "standard",
                    "@type": "ezread"
                  },
                  "{urn:abc.com/xmlns/mona/utterance}translation": {
                    "@text": "How much does it cost?",
                    "@lang": "en",
                    "@kind": "standard",
                    "@type": "translation"
                  },
                  "{urn:abc.com/xmlns/mona/utterance}native": {
                    "@text": "",
                    "@lang": "fr",
                    "@kind": "standard",
                    "@type": "native"
                  },
                  "{urn:abc.com/xmlns/mona/utterance}phonetic": {
                    "@text": "",
                    "@lang": "fr",
                    "@kind": "standard",
                    "@type": "phonetic"
                  },
                  "@anchor": "native"
                }
              }
            }
            ]

      }
    }

}

我需要访问@text属性,具体取决于@anchor属性中的值:

所以我的访问步骤是:
1.在课程变量中加载此JSON 2.获取根元素(lessonPages = lesson.root)
3.现在通过以下代码访问@anchor属性值:
text = lessonPages [“{urn:abc.com/xmlns/mona/page}”] [“{urn:abc.com/xmlns/mona/page} nut”] [“{urn:abc.com/ XMLNS /莫纳/页}说出 “] [”{瓮:abc.com/xmlns/mona/utterance}发声 “] [” @锚“]

现在我在文本变量中有@anchor值是“native”。现在如何使用text变量值来访问“{urn:abc.com/xmlns/mona/utterance} native”中的@text

注意:@anchor属性值不是fix.It可能是任何东西,所以我们需要先读取它然后@text取决于它。

1 个答案:

答案 0 :(得分:0)

它只是一个字符串。您可以正常连接字符串。

foo["{urn:abc.com/xmlns/mona/utterance}" + someStringVariable]