Reputation: 9
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
?
Upvotes: 0
Views: 66
Reputation: 3080
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
Upvotes: 1
Reputation: 281626
Suppose you have a JSON data as
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
}
}'
and you want to convert it into a JavaScript object i.e. an array format you can use
var data = JSON.parse(json);
You can use JSON.parse() to convert the data returned by the web API in array format provided the API return JSON data.
Upvotes: 2
Reputation: 784
Edit: This is for your question "How to create JSON object using jQuery?"
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 = $.parseJSON(json);
Upvotes: 2