0

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".

4
  • 2
    you should refer the following link stackoverflow.com/questions/10485014/… Commented Feb 21, 2014 at 6:41
  • can you please be more clear? Commented Feb 21, 2014 at 6:44
  • @yashhy Krishna have solved it.Thanks any way. Commented Feb 21, 2014 at 6:45
  • @zzy okay.. jsfiddle.net/yashhy/3T3cf refer this. This is what I was trying. Commented Feb 21, 2014 at 6:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

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.