方法一:拼接字符串
优点:自定义,灵活度高缺点:编码容易出差错,还要处理末尾的元素
string str = "{";for (int i = 0; i < list_head.Count; i++){str += "\\""+list_head[i]+"\\":[";var value=list.Where(x => x.TypeName == list_head[i]).ToList();for (int j = 0; j < value.Count; j++){if (j== value.Count-1){str += "{\\"ID\\":\\"" + value[j].ID + "\\",\\"Number\\":\\"" + value[j].Number + "\\",\\"TypeName\\":\\"" + value[j].TypeName + "\\"}";}else{str += "{\\"ID\\":\\"" + value[j].ID + "\\",\\"Number\\":\\"" + value[j].Number + "\\",\\"TypeName\\":\\"" + value[j].TypeName + "\\"},";}}if (i== list_head.Count-1){str += "]";}else{str += "],";}}str += "}";
方法二:使用第三方库(Newtonsoft)
优点:直接调用第三方封装好的方法,使用方便简洁缺点:有版本的区别(其实也不算缺点),没有方法一灵活,其他都比较好
//模型类,定义Json结构public class BaseModel{public List<ModelData> list_student { get; set; } = new List<ModelData>();public List<ModelData> list_teacher { get; set; } = new List<ModelData>();public List<ModelData> list_headteacher { get; set; } = new List<ModelData>();public List<ModelData> list_gradedirector { get; set; } = new List<ModelData>();public List<ModelData> list_deanr { get; set; } = new List<ModelData>();}
//实例化,存储数据BaseModel model = new BaseModel();model.list_student = list.Where(x => x.TypeName == "学生").ToList();model.list_teacher = list.Where(x => x.TypeName == "老师").ToList();model.list_headteacher = list.Where(x => x.TypeName == "班主任").ToList();model.list_gradedirector = list.Where(x => x.TypeName == "年级主任").ToList();model.list_deanr = list.Where(x => x.TypeName == "院长").ToList();string json2=JsonConvert.SerializeObject(model);
其他代码
//模型类public class ModelData{public ModelData(int iD, int number, string typeName){ID = iD;Number = number;TypeName = typeName;}public int ID { get; set; }public int Number { get; set; }public string TypeName { get; set; }}
//测试数据class Program{static void Main(string[] args){List<ModelData> list = new List<ModelData>(100);for (int i = 1; i <=100; i++){if (i<50){list.Add(new ModelData(i, 10, "学生"));}else if (i>=50&&i<75){list.Add(new ModelData(i, 20, "老师"));}else if (i >= 75 && i <85){list.Add(new ModelData(i, 30, "班主任"));}else if (i >= 85 && i < 95){list.Add(new ModelData(i, 40, "年级主任"));}else{list.Add(new ModelData(i, 40, "院长"));}}BaseModel model = new BaseModel();model.list_student = list.Where(x => x.TypeName == "学生").ToList();model.list_teacher = list.Where(x => x.TypeName == "老师").ToList();model.list_headteacher = list.Where(x => x.TypeName == "班主任").ToList();model.list_gradedirector = list.Where(x => x.TypeName == "年级主任").ToList();model.list_deanr = list.Where(x => x.TypeName == "院长").ToList();List<string> list_head = new List<string>() { "学生", "老师", "班主任", "年级主任","院长" };string str = "{";for (int i = 0; i < list_head.Count; i++){str += "\\""+list_head[i]+"\\":[";var value=list.Where(x => x.TypeName == list_head[i]).ToList();for (int j = 0; j < value.Count; j++){if (j== value.Count-1){str += "{\\"ID\\":\\"" + value[j].ID + "\\",\\"Number\\":\\"" + value[j].Number + "\\",\\"TypeName\\":\\"" + value[j].TypeName + "\\"}";}else{str += "{\\"ID\\":\\"" + value[j].ID + "\\",\\"Number\\":\\"" + value[j].Number + "\\",\\"TypeName\\":\\"" + value[j].TypeName + "\\"},";}}if (i== list_head.Count-1){str += "]";}else{str += "],";}}str += "}";Console.WriteLine(str);Console.WriteLine("-------------------------------------------------");string json2=JsonConvert.SerializeObject(model);Console.WriteLine(json2);Console.ReadKey();}
总结
方法二确实好,也有很多人使用,方法一也有自己的优点,如果格式唯一,改动小,可以直接使用方法二直接搞笑,如果格式经常需要改动,或者数据比较小的时候,用方法一,这个方便修改!主要是今天我用方法一做的程序,被老大说了,这种写法不好,直接否掉了,让我用第二种方法,在这记录一下!