我有一个包含电子邮件地址和状态数据成员的类对象列表。我试图将这些转换为json,确保在数组中有“操作”字。将对象列表转换为json数组
这是我的课:
class MyClass
{
public string email {get; set; }
public string status { get; set; }
}
这是我当前的代码(未建设):
List<MyClass> data = new List<MyClass>();
data = MagicallyGetData();
string json = new {
operations = new {
JsonConvert.SerializeObject(data.Select(s => new {
email_address = s.email,
status = s.status
}))
}
};
这是我试图获得JSON:
{
"operations": [
{
"email_address": "[email protected]",
"status": "good2go"
},
{
"email_address": "[email protected]",
"status": "good2go"
},...
]
}
EDIT1 我应该提到我得到的数据来自数据库。我从数据库反序列化一个JSON并以几种不同的方式使用这些数据,所以我不能更改我的类的成员名称。
什么是你实际上得到? – Arpit
为什么你对匿名对象感到厌烦?使用datacontrat代替或名称属性http://www.newtonsoft.com/json/help/html/JsonPropertyName.htm – cdie
@ Arpit最近我得到的是没有数组之前的操作字符串相同的输出。 – Blankdud