我正在嘗試為 tinymce 拼寫檢查器編寫自定義實現。我需要從我的 ashx 頁面返回格式的 JSON 對象{ "words": { "misspelled1": ["suggestion1", "suggestion2"], "misspelled2": ["suggestion1", "suggestion2"] }}其中 mispelled1 和 2 是拼寫錯誤的單詞及其各自的建議, words 是 id 所以一個實際的例子是{words:{"wod":["wood","wooden"],"tak":["take","taken"]}}我試過這個public class incorrectWords{ public string word { get; set; } public string[] suggestions { get; set; }}string json = Newtonsoft.Json.JsonConvert.SerializeObject(new { words= new List<incorrectWords>() { new words {word="wod",suggestions = new string[]{ "wood","wooden" } }, new words {word="tak",suggestions= new string[]{ "talk","take" } } } });context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(json,Newtonsoft.Json.Formatting.Indented)); }但是,這添加了屬性名稱單詞和建議,我最終得到以下不是我需要的。"{\"words\":[{\"word\":\"wod\",\"suggestions\":[\"wood\",\"wooden\"]},{\"word\":\"tak\",\"suggestions\":[\"talk\",\"take\"]}]}"在此先感謝您的任何指點。有些帖子似乎表明我需要一個自定義轉換器,我想知道設計不正確的Words類是否是一件簡單的事情
1 回答

炎炎設計
TA貢獻1808條經驗 獲得超4個贊
如果您想在 JSON 中有鍵值對,您應該將您的列表映射到字典。
這是您的代碼的修改版本,它可以工作:
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 關注
- 88 瀏覽
添加回答
舉報
0/150
提交
取消