2015-12-02 233 views
2

問題陳述陣列內的另一個陣列

我試圖創建這個數組內的另一個數組的數組,但它並沒有爲我工作。

這是我寫的:

var arr = '{"project":[' 
        + '{"id":"01","name":"project1","activity":[' 
        + '{"num":"001","time":"7","desc":"desc","stam":[' 
        + ' "pre":"005","pre2":"002"]}' 
        + '{"num":"002","time":"6","desc":"desc"}' 
        + '{"num":"003","time":"5","desc":"desc"}' 
        + '{"num":"004","time":"4","desc":"desc"}' 
        + '{"num":"005","time":"3","desc":"desc"}]}]}'; 
+0

這可能是有用的,我是新來的js所以不知道是否有任何幫助其http://stackoverflow.com/questions/9871634/javascript-array-inside-array-how-can- i-call-child-array-name –

+6

不要試圖通過將字符串砸在一起來構建JSON。調試太痛苦了。使用真正的數組和真實的對象,一旦你完成,使用'JSON.stringify'。 – Quentin

+3

也就是說,如果您將得到的字符串粘貼到http://jsonlint.com/(投票關閉,因爲問題是由簡單的印刷錯誤導致的),就會顯示您的小錯別字。 – Quentin

回答

4

你的JSON看起來損壞。您可以使用多個在線編輯器和驗證器來驗證JSON字符串。 editorvalidator只是其他mony的一個例子。你也可以看看here

  • ,的數組元素之間缺失
  • 財產stam看起來更像一個對象不是一個數組

它應該是這樣的:

{"project":[ 
      {"id":"01","name":"project1","activity": 
       [ 
       {"num":"001","time":"7","desc":"desc","stam":{ 
        "pre":"005", 
        "pre2":"002" 
       } 
       }, 
       {"num":"002","time":"6","desc":"desc"}, 
       {"num":"003","time":"5","desc":"desc"}, 
       {"num":"004","time":"4","desc":"desc"}, 
       {"num":"005","time":"3","desc":"desc"} 
       ] 
      } 
      ] 
} 
0

的JSON是不格式正確:

{ 
 
    project : [{ 
 
    id : "01", 
 
    name: "project1", 
 
    activity :[ 
 
     { 
 
     num : "001", 
 
     time : "7", 
 
     desc : "desc", 
 
     stam : [{ 
 
      pre : "005", 
 
      pre2: "002" 
 
     }] 
 
     }, 
 
     { 
 
     num : "002", 
 
     time: "6", 
 
     desc: "desc" 
 
     }, 
 
     { 
 
     num : "003", 
 
     time: "5", 
 
     desc:"desc" 
 
     }, 
 
     { 
 
     num : "004", 
 
     time: "4", 
 
     desc: "desc" 
 
     }, 
 
     { 
 
     num : "005", 
 
     time: "3", 
 
     desc: "desc" 
 
     } 
 
    ] 
 
    }] 
 
}