用c#创建JSON字符串
本文关键字:字符串 JSON 创建 | 更新日期: 2023-09-27 18:12:34
我是JSON格式的新手。我想创建以下使用c#, Json.net.
目标Json格式
{
"tafsir":{
"1_1":{
"text":"Some text here"
},
"1_2":{
"text":"Some text here2"
}
}
}
但是,得到输出:
"{'"tafsir'":{'"1_1'":'"Some text here'",'"1_2'":'"Some text here2'"}}
到目前为止,这并没有给创建所需的json字符串:
void Main()
{
var result = new Translation();
Dictionary<string, string> texts = new Dictionary<string, string>();
texts.Add("1_1", "Some text here");
texts.Add("1_2", "some text here2");
result.tafsir = texts;
var jsonStr = JsonConvert.SerializeObject(result);
}
public class Translation
{
public Dictionary<string, string> tafsir { get; set; }
}
public class Trans
{
public List<TransItem> Texts { get; set; }
}
public class TransItem
{
public string Id { get; set; }
public string Text { get; set; }
}
任何想法?
var obj = new {tafsir = new Dictionary<string, object>{
{"1_1", new{text="Some text here"}},
{"1_2", new{text="Some text here2"}}
}
};
var json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);