Browse Source

修改list 错误

master
赵鹏 1 day ago
parent
commit
65ffb344ae
  1. 156
      ReZero/SuperAPI/MethodGeneratorAPI/MethodGeneratorAPI.cs

156
ReZero/SuperAPI/MethodGeneratorAPI/MethodGeneratorAPI.cs

@ -255,7 +255,7 @@ namespace ReZero.SuperAPI
return; // 无法继续
}
// 获取第一个参数,但使用RawJsonData而不是参数值
// 获取原始JSON数据
var rawJson = dataModel.RawJsonData;
if (string.IsNullOrEmpty(rawJson))
{
@ -266,11 +266,13 @@ namespace ReZero.SuperAPI
System.Diagnostics.Debug.WriteLine($"使用JSON数据: {rawJson}");
var type = methodInfo.GetParameters().First().ParameterType;
// 处理JSON数据
if (!string.IsNullOrEmpty(rawJson))
// 如果JSON为空,无法继续
if (string.IsNullOrEmpty(rawJson))
{
System.Diagnostics.Debug.WriteLine("JSON数据为空,无法处理参数");
return;
}
try
{
// 检查原始JSON是否需要转义处理
@ -279,39 +281,155 @@ namespace ReZero.SuperAPI
rawJson = rawJson.Replace("\\\"", "\"");
}
// 尝试反序列化为目标类型
parameters[0] = Newtonsoft.Json.JsonConvert.DeserializeObject(rawJson, type);
System.Diagnostics.Debug.WriteLine($"成功将JSON反序列化为: {type.Name}");
// 解析JSON
JObject jsonObj = null;
try
{
jsonObj = JObject.Parse(rawJson);
System.Diagnostics.Debug.WriteLine($"成功解析JSON对象,属性数量: {jsonObj.Properties().Count()}");
}
catch
{
System.Diagnostics.Debug.WriteLine("JSON解析失败,尝试使用JsonConvert");
throw;
}
// 获取方法参数信息
var methodParams = methodInfo.GetParameters();
if (methodParams.Length == 1)
{
// 单参数方法的处理
var paramInfo = methodParams[0];
var paramName = paramInfo.Name;
var paramType = paramInfo.ParameterType;
System.Diagnostics.Debug.WriteLine($"参数名: {paramName}, 类型: {paramType.Name}");
// 检查JSON中是否存在与参数同名的属性
if (jsonObj.ContainsKey(paramName))
{
// JSON对象中有与参数同名的属性,使用该属性值
System.Diagnostics.Debug.WriteLine($"找到与参数同名的JSON属性: {paramName}");
JToken propValue = jsonObj[paramName];
// 转换为参数类型
try
{
if (propValue.Type == JTokenType.Array &&
(paramType.IsGenericType &&
paramType.GetGenericTypeDefinition() == typeof(List<>)))
{
// 处理数组到List的转换
string arrayJson = propValue.ToString();
parameters[0] = JsonConvert.DeserializeObject(arrayJson, paramType);
System.Diagnostics.Debug.WriteLine($"成功将数组属性 {paramName} 转换为List类型");
}
else
{
// 一般类型转换
parameters[0] = propValue.ToObject(paramType);
System.Diagnostics.Debug.WriteLine($"成功将属性 {paramName} 转换为 {paramType.Name} 类型");
}
}
catch (Exception propEx)
{
System.Diagnostics.Debug.WriteLine($"属性 {paramName} 转换为 {paramType.Name} 失败: {propEx.Message}");
// 如果属性转换失败,尝试使用整个JSON对象
try
{
parameters[0] = JsonConvert.DeserializeObject(rawJson, paramType);
System.Diagnostics.Debug.WriteLine($"回退到将整个JSON转换为 {paramType.Name}");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"JSON转换失败: {ex.Message}");
// 参数保持默认值
}
}
}
else
{
// JSON中没有与参数同名的属性,尝试直接转换整个JSON
System.Diagnostics.Debug.WriteLine($"未找到与参数 {paramName} 同名的JSON属性,尝试转换整个JSON");
try
{
// 尝试直接将JSON反序列化为参数类型
parameters[0] = JsonConvert.DeserializeObject(rawJson, paramType);
System.Diagnostics.Debug.WriteLine($"成功将JSON反序列化为: {paramType.Name}");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"JSON反序列化失败: {ex.Message}");
// 如果反序列化失败,尝试其他方式
try
// 如果反序列化失败,尝试JToken特殊处理
if (typeof(JToken).IsAssignableFrom(paramType))
{
if (type == typeof(Newtonsoft.Json.Linq.JObject))
if (paramType == typeof(JObject))
{
parameters[0] = Newtonsoft.Json.Linq.JObject.Parse(rawJson);
parameters[0] = jsonObj;
System.Diagnostics.Debug.WriteLine("直接使用解析后的JObject");
}
else if (type == typeof(Newtonsoft.Json.Linq.JToken))
else if (paramType == typeof(JToken))
{
parameters[0] = Newtonsoft.Json.Linq.JToken.Parse(rawJson);
parameters[0] = jsonObj;
System.Diagnostics.Debug.WriteLine("将JObject转换为JToken");
}
else
{
// 最后尝试直接转换或使用默认值
parameters[0] = Convert.ChangeType(rawJson, type);
System.Diagnostics.Debug.WriteLine($"无法处理的JToken子类型: {paramType.Name}");
}
}
catch (Exception innerEx)
else
{
System.Diagnostics.Debug.WriteLine($"参数转换失败: {innerEx.Message}");
// 保持默认值
System.Diagnostics.Debug.WriteLine($"无法将JSON转换为类型 {paramType.Name}");
}
}
}
}
else if (methodParams.Length > 1)
{
// 多参数方法的处理
System.Diagnostics.Debug.WriteLine($"多参数方法,参数数量: {methodParams.Length}");
for (int i = 0; i < methodParams.Length; i++)
{
var paramInfo = methodParams[i];
var paramName = paramInfo.Name;
var paramType = paramInfo.ParameterType;
// 查找JSON中是否有对应属性
if (jsonObj.ContainsKey(paramName))
{
try
{
JToken propValue = jsonObj[paramName];
parameters[i] = propValue.ToObject(paramType);
System.Diagnostics.Debug.WriteLine($"已绑定参数 {paramName} 为JSON属性值");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"参数 {paramName} 绑定失败: {ex.Message}");
}
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"FillJObjectParameters处理JSON时出错: {ex.Message}");
// 回退到原始方法
try
{
var type = methodInfo.GetParameters().First().ParameterType;
parameters[0] = JsonConvert.DeserializeObject(rawJson, type);
}
catch (Exception fallbackEx)
{
System.Diagnostics.Debug.WriteLine($"回退处理失败: {fallbackEx.Message}");
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"FillJObjectParameters异常: {ex.Message}");

Loading…
Cancel
Save