2012-04-30 9 views
0

になるこのJSONを再構築します:どのように私はそうのようにフォーマットされたいくつかのJSONデータを持っているよりアクセス

[ {"stage1" : [{ 
     "checkpoints" : 
      [ 
       { 
        "id" : "checkpoint1", 
        "name" : "a checkpoint name 1", 
        "purpose" : "a string about the purpose here", 
             }, 
       { 
        "id" : "checkpoint2", 
        "name" : "a checkpoint name 2", 
        "purpose" : "a string about the purpose here", 

       } 
      ],      

      "stages" : 
        [ 
        { 

        "id" : "an id here", 
        "name" : "a checkpoint name 1", 
        "purpose" : "a string about the purpose here yah", 

        } 
      ]   
     } 
    ]}, 

    {"stage2" : [{ 
     "checkpoints" : 
      [ 
       { 
        "id" : "checkpoint1", 
        "name" : "a checkpoint name 1", 
        "purpose" : "a string about the purpose here", 
             }, 
       { 
        "id" : "checkpoint2", 
        "name" : "a checkpoint name 2", 
        "purpose" : "a string about the purpose here", 

       } 
      ],      

      "stages" : 
        [ 
        { 

        "id" : "an id here", 
        "name" : "a checkpoint name 1", 
        "purpose" : "a string about the purpose here yah", 

        } 
      ]   
     } 
    ]}, 

    {"stage3" : [{ 
     "checkpoints" : 
      [ 
       { 
        "id" : "checkpoint1", 
        "name" : "a checkpoint name 1", 
        "purpose" : "a string about the purpose here", 
             }, 
       { 
        "id" : "checkpoint2", 
        "name" : "a checkpoint name 2", 
        "purpose" : "a string about the purpose here", 

       } 
      ],      

      "stages" : 
        [ 
        { 

        "id" : "an id here", 
        "name" : "a checkpoint name 1", 
        "purpose" : "a string about the purpose here yah", 

        } 
      ]   
     } 
    ]}, 





         ] 

現在、私は段階で取得するには、このようなデータを参照する必要があります。

alert(data[0].stage1[0].checkpoints.length); 

は、ステージ2データを取得するために私はこれをしなければならない:私はちょうど、ACCに芸名を使用している行うことができるようにしたいどのような

alert(data[1].stage2[0].checkpoints.length); 

「データ」の宣言後にインデックスを指定することなく、ESSデータ:

alert(data.stagex[0].checkpoints.length); 

私はデータ部分の後にインデックスを述べるために持っている必要はありません。 JSONを再構築するにはどうすればいいですか?最初にインデックスを指定せずに必要なデータを取得するためにステージ名を使用できますか?

+1

オブジェクトの配列は、それぞれプロパティ( 'stageX')が1つだけです。このプロパティを持つことはまったく必要ではないようですが、私はそれを削除するだけです。あるいは配列を持たずに、プロパティとして 'stage1'、' stage2'を持つ* one *オブジェクトを作成してください。 –

+0

また、チェックポイントの長さ属性はどのようにステージを参照していますか? – Amberlamps

答えて

3

Stage1、Stage2を配列に入れないでください。それらは直接キーとしてアクセスすることができます

+1

ニックヒルが言ったことを行うことによって、あなたは 'data.stage1 [0] .checkpoints'のリファレンスを短くすることができます – Nadh

+0

@nikhilどのように配列からステージを取り除くのですか?私はjson全体を囲む角括弧を削除しようとしましたが、これは無効なjsonを作成します –

+0

これは問題ありません。とった。ありがとう –

関連する問題