Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to add new obj of JSON like:

    "128": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "whtup"
        }]
    }

In the exist JSON object Example of JSON :

{
    "188": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "ki chal riha hai"
        }]
    },
    "123": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "whtup"
        }]
    },
    "128": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "whtup"
        }]
    }
share|improve this question
    
Please have look on this stackoverflow.com/questions/736590/… –  manishbagra Apr 2 '12 at 5:37
    
i want to add json obj in json this is for add data in array type of json obj –  pargan Apr 2 '12 at 5:50

1 Answer 1

up vote 8 down vote accepted

JSON stands for JavaScript object notation. So, it's nothing but an object ( actually a subset of object ) in javascript.

So, actually you want to add an object in existing javascript object.

Also, jQuery is nothing but a library (collections of different javascript functions to ease selecting dom elements, ajax functions, and some other utilities)

Coming back to your question,

If this is your existing object,

var obj = {
    "188": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "ki chal riha hai"
        }]
    },
    "123": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "whtup"
        }]
    },
    "128": {
        "Msg": [{
            "me": "hi"
        }, {
            "user": "hello"
        }, {
            "me": "whtup"
        }]
    }
}

You can add

  var objToAdd =  {
            "Msg": [{
                "me": "hi"
            }, {
                "user": "hello"
            }, {
                "me": "whtup"
            }]
        }

by,

obj["128"] = objToAdd;

Now, your obj is,

{
        "188": {
            "Msg": [{
                "me": "hi"
            }, {
                "user": "hello"
            }, {
                "me": "ki chal riha hai"
            }]
        },
        "123": {
            "Msg": [{
                "me": "hi"
            }, {
                "user": "hello"
            }, {
                "me": "whtup"
            }]
        },
        "128":{
            "Msg": [{
                "me": "hi"
            }, {
                "user": "hello"
            }, {
                "me": "whtup"
            }]
        }
    }
share|improve this answer
    
thanks for repy jashwat it work –  pargan Apr 2 '12 at 6:12
    
Thanks Jashwant :) –  KunJ Mar 27 '14 at 10:07
    
Wouldn't that just replace the value for key "128" (as opposed to creating a duplicate entry for it, as you've shown)? –  user2029783 Feb 11 at 15:45
    
@user2029783, you are right. Fixed. –  Jashwant Feb 11 at 18:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.