2

Problem Statement

I'm trying to create an array with another array inside this array, but it doesn't work for me.

This is what i wrote:

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"}]}]}';
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
laury
  • 83
  • 1
  • 1
  • 4
  • this may be useful, i am new to js so not sure if its of any help http://stackoverflow.com/questions/9871634/javascript-array-inside-array-how-can-i-call-child-array-name – Kumar Saurabh Dec 02 '15 at 11:19
  • 6
    Don't try to build JSON by smashing strings together. It is simply too painful to debug. Use real arrays and real objects and once you are done, use `JSON.stringify`. – Quentin Dec 02 '15 at 11:20
  • 3
    That said, your trivial typo is revealed if you take the resulting string and paste it into http://jsonlint.com/ (voting to close because the problem was caused by a simple typographical error). – Quentin Dec 02 '15 at 11:21

2 Answers2

4

Your JSON looks corrupted. You can use several online editors and validators to verify the JSON string. editor and validator just as an example of mony others. You also might have a look here.

  • , is missing between the array elements
  • the property stam looks more like an object than an array

It should look like this:

{"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"}
              ]
            }
          ]
}
frank
  • 1,217
  • 2
  • 10
  • 18
0

The json is not formatted correctly:

{
  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"
      }
    ]
  }]
}
John Roca
  • 1,204
  • 1
  • 14
  • 27