公司项目需要用到JSON来存储数据,于是使用了JSON.NET,记录一下,方便下一次使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| public struct DataStructInfo { public string Name; public string Title; public DataTypes Type; public int Length; public string Default; public bool IsNull; public bool IsIndex; public string Prefix; public string Suffix;
public DataStructInfo(string name, string title, DataTypes type, int length, string def, bool isNull, bool isIndex, string prefix, string suffix) { Name = name; Title = title; Type = type; Length = length; Default = def; IsNull = isNull; IsIndex = isIndex; Prefix = prefix; Suffix = suffix; } }
public class DataStructLogic { public static bool SaveStruct(List<DataStructInfo> dataStructs, string filePath) { if (dataStructs != null && !string.IsNullOrEmpty(filePath)) { var jsonObjs = JArray.FromObject(dataStructs); var jsonStr = jsonObjs.ToString().Replace("\r\n", "").Replace(" ", ""); StreamWriter SW; SW = File.CreateText(filePath + "\\struct.json"); SW.WriteLine(jsonStr); SW.Close(); return true; } return false; } public static List<DataStructInfo> ReadStruct(string filePath) { if (!string.IsNullOrEmpty(filePath)) { var jsonStr = File.ReadAllText(filePath + "\\struct.json"); JArray jsonObjs = JArray.Parse(jsonStr); List<DataStructInfo> dataStructs = jsonObjs.ToObject<List<DataStructInfo>>(); return dataStructs; } return null; }
|