c# - 将 json 反序列化为类
问题描述
我有以下 json 字符串:
[
{
"Key":"A",
"Value":null
},
{
"Key":"B",
"Value":"18"
},
{
"Key":"C",
"Value":"False"
},
{
"Key":"D",
"Value":"BOB"
}
]
我希望能够反序列化为以下对象:
public class ModelOne
{
public int? A { get; set; }
public int B { get; set;}
}
public class ModelTwo
{
public bool C { get; set; }
public string D { get; set; }
}
我们考虑过使用var model = JsonConvert.DeserializeObject<ModelOne>(json);
,但显然 json 字符串是键和值的列表,所以这行不通。
在理想情况下,我们希望解析 json 并将Key与Property Name匹配,并根据属性类型设置Value 。我们可以使用与上述类似的函数,它接受匿名类型,我们只是不确定从哪里开始,因此非常感谢一些反馈和/或帮助。
提前致谢。
编辑:
json 数组表示我们从外部 api 调用接收到的一些数据点。
ModelOne 和 ModelTwo 是我们想要预填充的 MVC 项目中的每个视图模型。
解决方案
非常感谢您的所有评论,尤其是@mjwills 和@Heretic Monkey,您确实提供了帮助。
最后(与我更好的判断相反)我决定使用一点反思。
public T ConvertDataMapToModel<T>(T item, List<Data> list)
{
Type itemType = typeof(T);
var response = (T)item;
var props = response.GetType().GetProperties();
foreach (var prop in props)
{
string propName = prop.Name;
string listValue = (string)(from c in list where c.Key == prop.Name select c.Value).FirstOrDefault();
if (!string.IsNullOrWhiteSpace(listValue) && !string.IsNullOrEmpty(listValue))
{
PropertyInfo pInstance = itemType.GetProperty(propName);
Type pInstancePropertyType = pInstance.PropertyType;
if (pInstancePropertyType.IsGenericType && pInstancePropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
pInstancePropertyType = pInstancePropertyType.GetGenericArguments()[0];
}
TypeCode typeCode = Type.GetTypeCode(pInstancePropertyType);
switch (typeCode)
{
case TypeCode.Boolean:
pInstance.SetValue(response, Convert.ToBoolean(listValue));
break;
case TypeCode.Byte:
pInstance.SetValue(response, Convert.ToByte(listValue));
break;
case TypeCode.Char:
pInstance.SetValue(response, Convert.ToChar(listValue));
break;
case TypeCode.DateTime:
pInstance.SetValue(response, Convert.ToDateTime(listValue));
break;
case TypeCode.DBNull:
pInstance.SetValue(response, Convert.DBNull);
break;
case TypeCode.Decimal:
pInstance.SetValue(response, Convert.ToDecimal(listValue));
break;
case TypeCode.Double:
pInstance.SetValue(response, Convert.ToDouble(listValue));
break;
case TypeCode.Empty:
pInstance.SetValue(response, "");
break;
case TypeCode.Int16:
pInstance.SetValue(response, Convert.ToInt16(listValue));
break;
case TypeCode.Int32:
pInstance.SetValue(response, Convert.ToInt32(listValue));
break;
case TypeCode.Int64:
pInstance.SetValue(response, Convert.ToInt64(listValue));
break;
case TypeCode.SByte:
pInstance.SetValue(response, Convert.ToSByte(listValue));
break;
case TypeCode.Single:
pInstance.SetValue(response, Convert.ToSingle(listValue));
break;
case TypeCode.String:
pInstance.SetValue(response, Convert.ToString(listValue));
break;
case TypeCode.UInt16:
pInstance.SetValue(response, Convert.ToUInt16(listValue));
break;
case TypeCode.UInt32:
pInstance.SetValue(response, Convert.ToUInt32(listValue));
break;
case TypeCode.UInt64:
pInstance.SetValue(response, Convert.ToUInt64(listValue));
break;
}
}
}
return response;
}
推荐阅读
- javascript - 推入存储在 JavaScript 对象值中的数组的奇怪行为
- ruby-on-rails - json 模板中的子节点
- python - 无法导入 ColumnTransfomer (Scikit-Learn) - 导入错误:无法导入名称“Parallel”
- c++ - 在循环中顺序遍历成员变量
- javascript - 为什么一键点击事件会映射到 3 个订阅者的 3 个不同值?
- javascript - 必须为我从 API 中提取的每篇文章创建一个单独的 div
- java - AlarmManager 不触发
- python - Python House Adventure - 如果您已经进入房间一次,如何提供不同的输出
- javascript - 我应该以什么格式存储我只需要“YYYY-MM-DD”部分的日期
- python - pyDES encrypt() 输出无法正确复制