2017-10-06 70 views
-1

我在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": {} } 
] 

如何插入歌名?

+6

你能更具體?這有點模糊......請參閱https://stackoverflow.com/help/mcve – benichka

+0

你需要一個字符串數組嗎? – 2017-10-06 09:13:59

+2

將'songtitle'更改爲集合。 '公開名單歌詞{get;組; }' – Fabio

回答

1

您必須修改屬性Songtitle並將其轉換爲stringList。然後,當您創建一個新的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); 
}