检测传入的json格式是否符合标准,并且对比json是否一样
c#代码
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 94 95 96 |
/// <summary> /// 检测传入的json格式是否符合标准 /// </summary> /// <param name="inobj">传入的json</param> /// <param name="obj">标准json</param> /// <param name="fathername"></param> /// <returns></returns> public static string ComparePropertiesJ(JObject inobj, JObject obj, string ordertype, string fathername = "") { //为空判断 if (inobj == null) return "数据不可以为空"; //根据开关删减标准json的字段 if (ordertype == td) { if (GetSystem_tb("IsNoFabricEnter")) //查询开关 obj.Remove("fabric"); } else if (ordertype == dh) { if (GetSystem_tb("IsNoModelEnter"))//查询开关 obj.Remove("style"); if (fathername == "") if (GetSystem_tb("IsNoFabricEnter")) //查询开关 obj.Remove("fabric"); } fathername = fathername == "" ? "" : fathername + " -> "; foreach (var item in obj.Children()) { //传入的当前字段的值 var obj1item = inobj[item.Path]; if (obj1item == null) return "缺失参数:" + fathername + item.Path; else if (obj1item.ToString() == "") return "参数为空:" + fathername + item.Path; else { //标准的当前字段的值 var standardson = obj[obj1item.Path]; if (standardson.Type != obj1item.Type) if (!(obj1item.Type == JTokenType.Integer && (standardson.Type == JTokenType.Float))) return fathername + item.Path + ":参数类型错误"; if (standardson.Type == JTokenType.Array) { if (obj1item.First == null) return "参数为空:" + fathername + item.Path; var standardsonfirst = standardson.First(); //如果标准的第一条是对象 且 传入的第一条也是对象 var asd = standardsonfirst.Type; for (int i = 0; i < obj1item.Count(); i++) { if (asd == JTokenType.Object && asd == obj1item[i].Type) { var aa1 = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(obj1item[i])); var aa2 = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(standardsonfirst)); string result = ComparePropertiesJ(aa1, aa2, ordertype, fathername + obj1item.Path + "[" + i + "]"); if (result != "ok") return result; } else { if (obj1item[i] == null) return "缺失参数:" + fathername + item.Path + "[" + i + "]"; else if (obj1item[i].ToString() == "") return "参数为空:" + fathername + item.Path + "[" + i + "]"; else { //标准的当前字段的值 if (standardsonfirst.Type != obj1item[i].Type) if (!(obj1item[i].Type == JTokenType.Integer && (standardsonfirst.Type == JTokenType.Float))) return "参数类型错误:" + fathername + item.Path + "[" + i + "]" ; } } } } else if (standardson.Type == JTokenType.Object) { var aa1 = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(obj1item)); var aa2 = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(standardson)); string result = ComparePropertiesJ(aa1, aa2, ordertype, fathername + obj1item.Path); if (result != "ok") return result; } } } return "ok"; } |
VB.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 |
Public Shared Function ComparePropertiesJ(ByVal inobj As JObject, ByVal obj As JObject, ByVal ordertype As String, ByVal Optional fathername As String = "") As String If inobj Is Nothing Then Return "数据不可以为空" If ordertype = td Then If GetSystem_tb("IsNoFabricEnter") Then obj.Remove("fabric") ElseIf ordertype = dh Then If GetSystem_tb("IsNoModelEnter") Then obj.Remove("style") If fathername = "" Then If GetSystem_tb("IsNoFabricEnter") Then obj.Remove("fabric") End If End If fathername = If(fathername = "", "", fathername & " -> ") For Each item In obj.Children() Dim obj1item = inobj(item.Path) If obj1item Is Nothing Then Return "缺失参数:" & fathername & item.Path ElseIf obj1item.ToString() = "" Then Return "参数为空:" & fathername & item.Path Else Dim standardson = obj(obj1item.Path) If standardson.Type <> obj1item.Type Then If Not (obj1item.Type = JTokenType.Integer AndAlso (standardson.Type = JTokenType.Float)) Then Return fathername & item.Path & ":参数类型错误" End If If standardson.Type = JTokenType.Array Then If obj1item.First Is Nothing Then Return "参数为空:" & fathername & item.Path Dim standardsonfirst = standardson.First() Dim asd = standardsonfirst.Type For i As Integer = 0 To obj1item.Count() - 1 If asd = JTokenType.Object AndAlso asd = obj1item(i).Type Then Dim aa1 = JsonConvert.DeserializeObject(Of JObject)(JsonConvert.SerializeObject(obj1item(i))) Dim aa2 = JsonConvert.DeserializeObject(Of JObject)(JsonConvert.SerializeObject(standardsonfirst)) Dim result As String = ComparePropertiesJ(aa1, aa2, ordertype, fathername & obj1item.Path & "[" + i & "]") If result <> "ok" Then Return result Else If obj1item(i) Is Nothing Then Return "缺失参数:" & fathername & item.Path & "[" + i & "]" ElseIf obj1item(i).ToString() = "" Then Return "参数为空:" & fathername & item.Path & "[" + i & "]" Else If standardsonfirst.Type <> obj1item(i).Type Then If Not (obj1item(i).Type = JTokenType.Integer AndAlso (standardsonfirst.Type = JTokenType.Float)) Then Return "参数类型错误:" & fathername & item.Path & "[" + i & "]" End If End If End If Next ElseIf standardson.Type = JTokenType.Object Then Dim aa1 = JsonConvert.DeserializeObject(Of JObject)(JsonConvert.SerializeObject(obj1item)) Dim aa2 = JsonConvert.DeserializeObject(Of JObject)(JsonConvert.SerializeObject(standardson)) Dim result As String = ComparePropertiesJ(aa1, aa2, ordertype, fathername & obj1item.Path) If result <> "ok" Then Return result End If End If Next Return "ok" End Function |
以上 ordertype 参数可以去掉