可以使用Dictionary<string, object>
將數組作爲值簡單生成此響應。
public class KeywordTitle
{
public string keyword { get; set; }
public string title { get; set; }
}
public class Response
{
public string error { get; set; }
public string message { get; set; }
public Dictionary<string, object> data { get; set; }
}
var dictionary = new Dictionary<string, object> {
{"version", "sring"}
};
dictionary.Add("1", new []
{
new KeywordTitle { keyword = "", title = "" },
new KeywordTitle { keyword = "", title = "" },
new KeywordTitle { keyword = "", title = "" }
});
JsonConvert.SerializeObject(new Response
{
error = "0",
message = "messages",
data = dictionary
});
它產生:
{
"error" : "0",
"message" : "messages",
"data" : {
"version" : "sring",
"1" : [{
"keyword" : "",
"title" : ""
}, {
"keyword" : "",
"title" : ""
}, {
"keyword" : "",
"title" : ""
}
]
}
}
如果這是你的API,那麼它是一個好主意,以便使所有對象在data
是同一類型的提取version
,和類型的鑰匙int
。
你想讓他們成爲1,2,3等任何特定的原因? –
我認爲newtonsoft.json dll幫助.. – null1941