How to create JSON object into array using jQuery?

How to create JSON Object using jQuery?

I have a JSON Object in below format:

{

    "1":{
        "c_roleid":null,
        "ObjectID":1,
        "c_appname":"Default",
        "c_display":true,
        "c_add":null,
        "c_edit":null,
        "c_delete":null,
        "c_execute":null,
        "c_isdefault":null,
        "c_rolepermissionid":null,
        "c_objecttype":1
    }
}

How can I create JSON object in array for above format.

How to get data as array format into web API?

Answers 1

  • var json = '{"1":{"c_roleid":null,"ObjectID":1,"c_appname":"Default","c_display":true,"c_add":null,"c_edit":null,"c_delete":null,"c_execute":null,"c_isdefault":null,"c_rolepermissionid":null,"c_objecttype":1}}';
    var obj = JSON.parse(json);
    

    Now you can access your JSON-array like this:

    for(var key in obj){    
       console.log(obj[key].ObjectID); // will print 1 
    }
    

    In the for-loop you can access every property, like for example "c_roleid", c_appname", etc. For easier readability:

    for(var key in obj){
    var prop = obj[key],
        roleID = prop.c_roleid,
        objectID = prop.ObjectID,
        appName = prop.c_appname,
        // ...continue with variables 
        objectType = prop.c_objecttype;
    }
    

    For more examples see this url: Serializing to JSON in jQuery


Related Articles