是否可以得到这样的格式:
"list":{
"one":{
"fieldone":"-----",
"fieldtwo":"-----"
},
"two":{
"fieldone":"-----",
"fieldtwo":"-----"
}
}
我不希望数组[]或名称"one"one_answers"two"被删除,我正在努力获得确切的以下格式。
如果你把你的项目放入字典而不是列表中,那么你可以得到你想要的结果。下面是一个例子:
class Program
{
static void Main(string[] args)
{
RootObject obj = new RootObject();
obj.Items = new Dictionary<string, Item>();
obj.Items.Add("one", new Item { FieldOne = "foo", FieldTwo = "bar" });
obj.Items.Add("two", new Item { FieldOne = "fizz", FieldTwo = "bang" });
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
Console.WriteLine(json);
}
}
class RootObject
{
[JsonProperty("list")]
public Dictionary<string, Item> Items { get; set; }
}
class Item
{
[JsonProperty("fieldone")]
public string FieldOne { get; set; }
[JsonProperty("fieldtwo")]
public string FieldTwo { get; set; }
}
输出:{
"list": {
"one": {
"fieldone": "foo",
"fieldtwo": "bar"
},
"two": {
"fieldone": "fizz",
"fieldtwo": "bang"
}
}
}