0

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?

2
  • 1
    show what you have tried so far? Commented Apr 8, 2016 at 3:45
  • This question is not possible to parse in English. Maybe not best language but sadly is what we parse. Please try to communicate what your goal is. Array of what? Array in JSON? Array in JavaScript? Why you want array? For what? Commented Apr 8, 2016 at 3:53

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Ok. I do it. But How I get data from Web Api in Array Format?
I don't understand what that means. You can parse JSON from a web API like this: $.getJSON("url_with_json_here", function(data){ /* do something */ });. What do you mean by Array Format?
2

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.

Comments

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

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.