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
asked Apr 18, 2011 at 8:18
user663724
-
Remember that arrays start at 0--data[1] is the last element; data[2] would be undefined.Tikhon Jelvis– Tikhon Jelvis2011年04月18日 08:21:07 +00:00Commented Apr 18, 2011 at 8:21
-
learn JSONxkeshav– xkeshav2011年04月18日 08:22:29 +00:00Commented Apr 18, 2011 at 8:22
1 Answer 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"}
]
};
answered Apr 18, 2011 at 8:23
Guffa
703k112 gold badges760 silver badges1k bronze badges
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
Guffa
@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.
xkeshav
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 .
|
lang-js