我在 Asp.net 的通用处理程序中有以下代码:
var students= Student.GetStudents();
var result = new
{
Data = students.Select(s => new []{s.subjects})
};
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = serializer.Serialize(result);
context.Response.ContentType = "application/json";
context.Response.Write(json);
应用于班级Student
:
public class Student
{
public List<String> subjects{get; set;}
Student()
{
subjects= new List<string>();
}
public static IEnumerable<Student> GetStudents()
{
List<Student> students= new List<Student>();
Student s1 = new Student();
s1.subjects.Add("Trigonometry");
s1.subjects.Add("Chemistry");
students.Add(row1);
Student s2 = new Student();
s2.subjects.Add("Physics");
s2.subjects.Add("Biology");
students.Add(s2);
return students;
}
}
这给了我输出:
{"Data":[[["Trigonometry","Chemistry"]],[["Physics","Biology"]]]}
但我希望它采用以下格式:
{"Data":[["Trigonometry","Chemistry"],["Physics","Biology"]]}
JavaScriptSerializer 是如何工作的?
为什么要创建另一个数组?
我应该怎么做才能解决这个问题?