0

From AJAX Call i am constructing this data in my server

{data:[{one:"1",two:"2"},{one:"3",two:"3"}]}

I am able to access this data using data.jobs[2].one;

My question is that , is it possible to construct a similar array inside javascript also I mean this way :

var data = [{one:1,two:2},{one:3,two:3}];

Please help , thank you very much

2
  • Remember that arrays start at 0--data[1] is the last element; data[2] would be undefined. Commented Apr 18, 2011 at 8:21
  • learn JSON Commented Apr 18, 2011 at 8:22

1 Answer 1

1

To access a value using data.jobs[2].one you would need a structure like this:

var data = {
  jobs:[
    {one:"1",two:"2"},
    {one:"3",two:"3"}
  ]
};
Sign up to request clarification or add additional context in comments.

7 Comments

You misunderstood my question , i am sorry for the confusion caused . I would like to dynamically create this var data = { jobs:[ {one:"1",two:"2"}, {one:"3",two:"3"} ] }; for example , i will get this values from my database so dependng upon for(var i = 0 ; i<data.jobs.length ; i++) , i want to construct this javascript array
anybody please help , is this possible to do so ??
@user663724: You would have to provide more details, like what language and platform you are using on the server side, and in what form you have got the data from the database.
please tell us what output from database u getting and which programming language u useing??
Thank you Guffa for the response , i am using Java(Servlets ) on server side StringBuffer jobs = new StringBuffer("{jobs:["); while (rs.next()) { jobs.append("{one:\"" + rs.getString("one") + "\",two:\"" + rs.getString("two") + "\"},"); } jobs.setCharAt(jobs.length() - 1, ']'); jobs.append("}"); This is how i am constructing Response on server side .
|

Your Answer

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