LoginSignup
2
3

More than 5 years have passed since last update.

javascriptで@や#をつかったJSON要素を参照する

Last updated at Posted at 2015-05-20

js2xmlparserというnode packageを使ってJSON->xmlへの変換が便利だが、xmlとの整合性のために独特の記法をつかう、

属性を表現するために、@を用い、要素は#で表現する

<fruit>
  <strawberry color="red">sweet</strawberry>
  <grape color="violet">sour</grape>
<fruit>

上記は

"fruit":{
  strawberry:{
    "@":{"color":"red"},
    "#":"sweet"
  },
  grape:{
    "@":{"color":"violet"},
    "#":"sour"
  }
}

jsonでこの様に表現できる記法。
ここで、@や#のような要素にどうやってアクセスするんだ?というところではまる

以下はエラーがでる

taste = fruit.strawberry.#

こうやるといける

taste = fruit.strawberry["#"]

ご参考まで

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3