I am facing issues while constructing an object using javascript. I want this:
{
"p_id": "2",
"p_name": "weblogic",
"ip_list": [
{
"ip_id": 2690
},
{
"ip_id": 2692
},
{
"ip_id": 2693
}
]
}
Below is the javascript code that I am using to get the data into the object:
var ipArray = [];
secTagJSON.p_name = "weblogic";
secTagJSON.p_id = "2";
for (var index=0; index < selectedArray.length; index++){
secTagJSON.ip_list.push("ip_id": selectedArray[index]);
}
I am able to construct the properties for p_id and p_name but struggling to create the the ip_list. Please let me know how to get this constructed using javascript.
Code for posting to the server:
var ipArray = [];
secTagJSON.p_name = "weblogic";
secTagJSON.p_id = 2;
for (var index=0; index < selectedArray.length; index++){
secTagJSON.ip_list.push({"ip_id": selectedArray[index]});
}
console.log (secTagJSON);
console.log (JSON.stringify(secTagJSON));
$http.post("http://server:port/api/v1/tags").
success(function(data) {
console.log (data)
});
-
What you just posted is a valid way to construct it using Javascript.Paul– Paul2014年03月27日 18:02:34 +00:00Commented Mar 27, 2014 at 18:02
-
Hmmm . What you mean by constructing ? ... because i suspect that something is terribly wrong by your wordsSergeS– SergeS2014年03月27日 18:03:04 +00:00Commented Mar 27, 2014 at 18:03
-
1How are you creating the json?DontVoteMeDown– DontVoteMeDown2014年03月27日 18:03:06 +00:00Commented Mar 27, 2014 at 18:03
-
Please find the below code that I have written to construct the json message: var ipArray = []; secTagJSON.p_name = "weblogic"; secTagJSON.p_id = "2"; for (var index=0; index < selectedArray.length; index++){ secTagJSON.ip_list.push("ip_id": selectedArray[index]); } But I am not getting in the desired format as mentioned in my questionPradeep– Pradeep2014年03月27日 18:05:32 +00:00Commented Mar 27, 2014 at 18:05
-
1@JAAulde made a simple but important observation.Walter Macambira– Walter Macambira2014年03月27日 18:21:58 +00:00Commented Mar 27, 2014 at 18:21
2 Answers 2
Simply do this:
var obj = { ip_list: [] };
obj.p_name = "weblogic";
obj.p_id = "2";
for (var i = 0, j = selectedArray.length; i < j; i++)
obj.ip_list.push({ ip_id: selectedArray[i] });
Note that your ip_list is actually an array of objects. So, when you iterate over it, remember that each var item = json.ip_list[i] will return an object, that you can access its properties using: item['ip_id'].
Note that obj is an Javascript object, it is not an JSON. If you want the JSON, you can use JSON.stringify(obj). This will return your JSON (string).
Hope I've helped.
2 Comments
Try:
secTagJSON.p_name = "weblogic";
secTagJSON.p_id = "2";
secTagJSON.ip_list = [];
for (var index=0; index < selectedArray.length; index++){
secTagJSON.ip_list.push({"ip_id": selectedArray[index]});
}
you forgot your {} around "ip_id": etc...
You also need to declare that ip_list is an array.
Your ip_list is an array of objects. I would guess that your script was not running as it was.
Posting to your server you should use:
$http.post('server:port/api/v1/tags', secTagJSON).sucess(...