Plotting JSON in Javascript

I am trying to build a JSON object in js. There are several posts already about this topic on Stack Overflow. Applies to How do I dynamically plot JSON in javascript? ... Should construct JSON exactly as mentioned in the post.

{
    "privilege": {
        "accesstype": "VIEW",
        "attribute": [
            {
                "code": "contenttype",
                "displayname": "Content type",
                "value": {
                    "valcode": "book_article",
                    "valdisplayName": "Book Article"
                }
            },
            {
                "code": "mime",
                "displayname": "Mime type",
                "value": {
                    "valcode": "xml",
                    "valdisplayName": "Xml"
                }
            }
        ]
    }
}

      

Got answers for this post and tried this,

var privilege = {};

privilege.attribute[0].code = "contenttype";
privilege.attribute[0].displayname = "Content type";
privilege.attribute[0].value.valcode = "book_article";
privilege.attribute[0].value.valdisplayName = "Book Article";

      

But getting error as privilege.attribute is undefined.

I cannot figure out where I am going wrong. Assuming there must be some problems with the declaration. Any light on this will be very helpful.

+3


source to share


3 answers


Try the following:

 var privilege = {};
 privilege.attribute = [];
 privilege.attribute[0] = {};
 privilege.attribute[0].code="contenttype";
 privilege.attribute[0].displayname="Content type";
 privilege.attribute[0].value = {};
 privilege.attribute[0].value.valcode="book_article";
 privilege.attribute[0].value.valdisplayName="Book Article";

      



Have a look at the Javascript object. There's a lot there. For example http://mckoss.com/jscript/object.htm

Edit : Initialized privilege.attribute[0] = {};

after the hint in the comment.

+4


source


Why not just do

var privilege = {
    "accesstype": "VIEW",
    "attribute": [{
        "code": "contenttype",
        "displayname": "Content type",
        "value": {
            "valcode": "book_article",
            "valdisplayName": "Book Article"
        }
    }, {
        "code": "mime",
        "displayname": "Mime type",
        "value": {
            "valcode": "xml",
            "valdisplayName": "Xml"
        }
    }]
}

      



... you don't actually need keys to be strings, you could write ...

var privilege = {
    accesstype: "VIEW",
    attribute: [{
        code: "contenttype",
        displayname: "Content type",
        value: {
            valcode: "book_article",
            valdisplayName: "Book Article"
        }
    }, {
        code: "mime",
        displayname: "Mime type",
        value: {
            valcode: "xml",
            valdisplayName: "Xml"
        }
    }]
}

      

+1


source


try it

privilege.attribute = [];
privilege.attribute.push({});

privilege.attribute[0].code="contenttype";
privilege.attribute[0].displayname="Content type";
privilege.attribute[0].value.valcode="book_article";
privilege.attribute[0].value.valdisplayName="Book Article";

      

0


source







All Articles