我在C#中遇到数组或JSON问题。问题是,我想在现有对象中插入数组。
class Bands
{
public string BandName { get; set; }
public string Songtitle { get; set; }
}
public JsonResult Band()
{
var items = new Bands()
{
BandName = "amber",
Songtitle = //how to add array here;
};
return Json(items, JsonRequestBehavior.AllowGet);
}
输出:
[
{ "bandname": "maroon", "songtitle": {} },
{ "bandname": "amber pacific", "songtitle": {} }
]
如何插入歌曲标题?
答案 0 :(得分:1)
您必须修改属性Songtitle
并将其转换为List
string
。然后,当您创建新的Band
时,可以根据需要添加许多歌曲。
以下代码:
private class Bands
{
public string BandName { get; set; }
public List<string> Songtitles { get; set; }
}
public JsonResult Band()
{
var items = new Bands()
{
BandName = "amber",
Songtitles = new List<string> { "song 1", "song 2" }
};
return Json(items, JsonRequestBehavior.AllowGet);
}