I want to create Json like this:
{
"name":"name",
"children":[
{
"childName":"name"
},
{
"childName":"name"
}
]
}
I don't know how to place none-named property in json obj and place and obj into "children".
-
2you should refer the following link stackoverflow.com/questions/10485014/…user3305314– user33053142014年02月21日 06:41:21 +00:00Commented Feb 21, 2014 at 6:41
-
can you please be more clear?yashhy– yashhy2014年02月21日 06:44:38 +00:00Commented Feb 21, 2014 at 6:44
-
@yashhy Krishna have solved it.Thanks any way.zzy– zzy2014年02月21日 06:45:57 +00:00Commented Feb 21, 2014 at 6:45
-
@zzy okay.. jsfiddle.net/yashhy/3T3cf refer this. This is what I was trying.yashhy– yashhy2014年02月21日 06:47:58 +00:00Commented Feb 21, 2014 at 6:47
2 Answers 2
OK, if you mean key itself is variable then you cannot create json-object in single shot, you will have to create it using '[]' notation
var myObj = {};
myObj[myProp1] = [] //or some value or some json/map again
myObj[myProp2] = 'hi'
myProp1 and myProp2 are variables.if you can explain your problem in more detail then you will get more clear answer.
Comments
If you ask how to manipulate that JSON object, then maybe this would help.
Your original object:
var object = {
"name":"name",
"children":[
{
"childName":"name"
},
{
"childName":"name"
}
]
};
1) How to place none-named property in json obj.
Object is not array, so should assign key/value, though either or both of them were empty. You can insert using dot or like array assignment.
object.country = "Malaysia";
object[""] = "No Keys";
object["novalue"] = "";
2) How to place an obj into "children".
var aNewChild = {
"childName": "Handsome one"
};
object.children.push(aNewChild);