我的JSON對象中屬性的名稱需要通過字符串進行分配,我怎樣才能用JObject來實現?或者我應該只是從字符串JObject.parse?我知道.Net Json文檔,但是它非常基本,並且不會顯示如下所示的任何示例。JSON指定帶字符串變量的JObject屬性名稱
這是我現在有:
return JObject.FromObject(new {
attachment = new {
type = "template",
payload = new {
template_type = "button",
text = Title,
buttons = new {
type = type,
Variable1 = Value,
Variable2 = Payload
}
}
}
});
另外,如果我不喜歡它下面的string.parse方式,這是格式化字符串的最好方法?
JObject.Parse(@"{
attachment : {
type : 'template',
payload : {
template_type : 'button',
text : '"[email protected]"',
buttons : [{
type : '"[email protected]"',
"+Variable1+" : '"[email protected]"',
"+Variable2+" : '"[email protected]"'
}]
}
}
}"
我不完全相信我跟隨的問題。在第一個例子中,什麼是用作屬性名稱的字符串? – dbc
使用'Dictionary' –
@dbc因此,在第二個示例中,Variable1和Variable2可以很好地分配,因爲它是一個簡單的字符串。但是,在第一個示例中,我無法將屬性名稱variable1和variable2分配給實際變量,因此在運行代碼時,名稱爲Variable1而不是其值。 – james