在 Javascript 中构建 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14752606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:29:43  来源:igfitidea点击:

Constructing JSON in Javascript

javascriptjson

提问by NitZRobotKoder

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.

我正在尝试在 js 中构造 JSON 对象。在堆栈溢出本身中已经有几篇关于这个主题的帖子。参考如何在 javascript 中动态构建 JSON?. 必须完全像帖子中提到的那样构造 JSON。

{
    "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.

我无法弄清楚我哪里出错了。假设肯定存在一些声明问题。任何关于它的灯都会有很大帮助。

回答by devOp

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.com/jscript/object.htm

看看 Javascript 对象。那里有很多东西。例如http://mckoss.com/jscript/object.htm

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

编辑privilege.attribute[0] = {};在评论提示后初始化。

回答by Jim Blackler

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

回答by Salman

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";