我有一个如下所示的对象模型:
public class Myclass
{
public int Id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
public ContentResult GetList(List<Myclass> model)
{
var list = JsonConvert.SerializeObject(
model,
Formatting.Indented,
new JsonSerializerSettings()
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});
return Content(list, "application/json");
}
我需要OUTPUT:
[[1,"name1",23],[2,"name2",30],[3,"name3",26],[4,"name4",29]]
答案 0 :(得分:-2)
如果满足您的需求,您可以使用此解决方法,如果有效,请告诉我
List<Myclass> model = new List<Myclass>();
model.Add(new Myclass() { Id = 1, Name = "Name1", Age = 50 });
model.Add(new Myclass() { Id = 2, Name = "Name2", Age = 51 });
model.Add(new Myclass() { Id = 3, Name = "Name3", Age = 52 });
string json = JsonConvert.SerializeObject(model);
//If you want to replace { with [ and } with ]
json = json.Replace("{", "[").Replace("}", "]");
//you can use this workaround to get rid of property names
string propHeader = "\"{0}\":";
json= json.Replace(string.Format(propHeader, "Id"), "")
.Replace(string.Format(propHeader, "Name"),"")
.Replace(string.Format(propHeader, "Age"), "");
Console.WriteLine(json);