/// <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";
}