我正在使用犀牛。我想使用变量来定义地图的键,如下所示:
var name = "Ryan";
var relationshipType = "brother";
var relatedName = "David";
var json = {
"name": name,
"relationships": {
relationshipType: relatedName
}
};
当我解析这个json,键" relationshipType"时,它是文字而不是定义的变量,它解析为" brother"。有什么想法吗?
" relationshipType未被解析为"兄弟",如下面的输出所示:
{
"name": Ryan,
"relationships": {
relationshipType: David
}
};
答案 0 :(得分:3)
您可以使用bracket notation,就像这样
var grouped = from d in db.MyTable
group d by new {
User = d.User,
Date=d.Date.HasValue ? d.Date.Value.Date : (DateTime?)null} into g
select new {User=g.Key.User, Date=g.Key.Date, Count=g.Count()};
如果您可以使用ES6语法,那么您可以像这样解决这个问题
var name = "Ryan";
var relationshipType = "brother";
var relatedName = "David";
var json = {
"name": name,
"relationships": {}
};
json[relationshipType] = relatedName;
console.log(json);