这可能是一个菜鸟问题,但如果我想制作一个JSON项目列表(在nodejs应用程序中),我可以执行以下操作:
var myVar = {
'title' : 'My Title',
'author' : 'A Great Author'
};
console.log(JSON.stringify(myVar));
OUTPUT: { 'title' : 'My Title', 'author' : 'A Great Author' }
并且一切都很好,但我如何制作如下的子列表?
OUTPUT: { book {'title' : 'My Title', 'author' : 'A Great Author'} }
答案 0 :(得分:2)
{}
是对象文字语法,propertyName: propertyValue
定义属性。继续把它们筑巢。
var myVar = {
book: {
'title' : 'My Title',
'author' : 'A Great Author'
}
};
答案 1 :(得分:1)
使用JavaScript执行此操作:
var mVar = {
'title' : 'My Title',
'author' : 'A Great Author'
};
var myVar = {};
myVar.book = mVar;
console.log(JSON.stringify(myVar));
请参阅:http://jsfiddle.net/JvFQJ/
使用对象文字表示法来执行此操作:
var myVar = {
'book': {
'title' : 'My Title',
'author' : 'A Great Author'
}
};
console.log(JSON.stringify(myVar));
答案 2 :(得分:0)
如此:
var myVar = {
'book': {
'title' : 'My Title',
'author' : 'A Great Author'
}
};
答案 3 :(得分:0)
你会做这样的事情:
myVar = {
book: {
'title' : 'My Title',
'author' : 'A Great Author'
}
}
console.log(JSON.stringify(myVar)); // OUTPUT: { book {'title' : 'My Title', 'author' : 'A Great Author'} }
如果您想要子列表中的多个项目,请将其更改为:
myVar = {
book1: {
'title' : 'My Title',
'author' : 'A Great Author'
},
book2: {
'title' : 'My Title',
'author' : 'A Great Author'
}
}