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"}]}]}';
3
  • this may be useful, i am new to js so not sure if its of any help stackoverflow.com/questions/9871634/… Commented Dec 2, 2015 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. Commented Dec 2, 2015 at 11:20
  • 3
    That said, your trivial typo is revealed if you take the resulting string and paste it into jsonlint.com (voting to close because the problem was caused by a simple typographical error). Commented Dec 2, 2015 at 11:21

2 Answers 2

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"}
              ]
            }
          ]
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.