最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Constructing JSON in Javascript - Stack Overflow

programmeradmin2浏览0评论

I am trying to construct JSON object in js. There are couple of posts already about this topic in stack overflow itself. Referred to How do i build JSON dynamically in javascript?. Have to construct JSON exactly something like 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"
                }
            }
        ]
    }
}

Follwed the answers in the 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 undefined.

I am not able to figure out where I am going wrong. Assuming there must be some declaration problems. Any light on it would be of great help.

I am trying to construct JSON object in js. There are couple of posts already about this topic in stack overflow itself. Referred to How do i build JSON dynamically in javascript?. Have to construct JSON exactly something like 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"
                }
            }
        ]
    }
}

Follwed the answers in the 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 undefined.

I am not able to figure out where I am going wrong. Assuming there must be some declaration problems. Any light on it would be of great help.

Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Feb 7, 2013 at 13:44 NitZRobotKoderNitZRobotKoder 1,0869 gold badges45 silver badges76 bronze badges 4
  • json/js.html – mdn Commented Feb 7, 2013 at 13:47
  • 3 Have you tried privilege.attribute = new Array() or something like that before adding the members to it? I think it has to do with the fact that attribute itself is not being declared and you're immediately trying to use it as an array. Just a guess though. – jonhopkins Commented Feb 7, 2013 at 13:48
  • Well, you have to initialize privilege.attribute as an array and the first element of that array as an object. Please have a look at developer.mozilla/en-US/docs/JavaScript/Guide/…. Once you built the object, you can use JSON.stringify to convert it to JSON. – Felix Kling Commented Feb 7, 2013 at 13:51
  • 1 possible duplicate of How do i build JSON dynamically in javascript? -- the answer shows everything you need to know, you just did not follow it properly. Please read it again. That being said, depending on what you actually want to do, there might be much easier ways to create the structure you want (see Jim's answer). – Felix Kling Commented Feb 7, 2013 at 13:56
Add a ment  | 

3 Answers 3

Reset to default 9

Try this:

 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 Javascript Object. There is a lot of stuff out there. E.g http://mckoss./jscript/object.htm

Edit: Initialized privilege.attribute[0] = {}; after hint in ment.

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"
        }
    }]
}

... in fact, you don't need the 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"
        }
    }]
}

Try this

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)

  1. 暂无评论