另一个数组内的数组

时间:2015-12-02 11:14:33

标签: javascript arrays json

问题陈述

我试图在这个数组中创建一个包含另一个数组的数组,但它并不适合我。

这就是我写的:

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"}]}]}';

2 个答案:

答案 0 :(得分: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"}
              ]
            }
          ]
}

答案 1 :(得分: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"
      }
    ]
  }]
}