1 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果您想在 JSON 中有鍵值對(duì),您應(yīng)該將您的列表映射到字典。
這是您的代碼的修改版本,它可以工作:
var words = new List<incorrectWords>() {
new incorrectWords() {word="wod",suggestions = new string[]{ "wood","wooden" } },
new incorrectWords() {word="tak",suggestions= new string[]{ "talk","take" } }
};
var dic = new Dictionary<string, string[]>();
words.ForEach(word =>
{
dic.Add(word.word, word.suggestions);
});
string json = Newtonsoft.Json.JsonConvert.SerializeObject(new {
words = dic
});
context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented));
- 1 回答
- 0 關(guān)注
- 87 瀏覽
添加回答
舉報(bào)