【发布时间】:2014-04-17 20:56:00
【问题描述】:
我在使用 JavascriptDeserializer 或 NewtonJSON 在 c# 中反序列化 JSON 对象的嵌套数组时遇到问题。我在这里到处寻找并尝试了建议的修复,但它一直给我这个: 编辑添加了完整的响应错误
{
"Message": "Type 'System.String' is not supported for deserialization of an array.",
"StackTrace": " at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer
serializer, Boolean throwOnError, IList& convertedList)
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer
serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer
serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n
at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData,
IDictionary`2 rawParams)\r\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
"ExceptionType": "System.InvalidOperationException"
}
它发送到网络服务的 JSON 最终看起来像这样:
{
"ProfileEntries": [
{
"TypeId": "C",
"CodeId": "HEARTDZ ",
"StudComment": "testComment"
},
{
"TypeId": "C",
"CodeId": "HIBLOOD "
},
{
"TypeId": "F",
"CodeId": "MARFANF ",
"StudComment": "testComment"
},
{
"TypeId": "F",
"CodeId": "MITRALF "
}
]
}
您看到一些没有“StudComment”的对象的原因是因为我使用敲除来保存加载时最初不是对象一部分的值。
我的网络服务基本上这样做是为了获取反序列化 JSON 编辑添加的方法名称的列表
public void SaveProfileEntries(string ProfileEntries)
{
var healthInfoProfileEntries = new JavaScriptSerializer().Deserialize<HealthEntries>(ProfileEntries);
这些是我用来反序列化数据的类:
public class HealthEntries
{
[JsonProperty("ProfileEntries")]
public List<HealthEntry> ProfileEntries { get; set; }
}
public class HealthEntry
{
[JsonProperty("TypeId")]
public string TypeId { get; set; }
[JsonProperty("CodeId")]
public string CodeId { get; set; }
[JsonProperty("StudComment")]
public string StudComment { get; set; }
}
这很令人困惑,因为当我使用 VS 单元测试对其进行测试时,它运行良好,但是当我在我的网站中使用它时,它给了我相同的 InvalidOperationException
我已经用这种方法尝试过 NewtonJSON,但它也不起作用
var healthInfoProfileEntries = (HealthEntries)JsonConvert.DeserializeObject(ProfileEntries, typeof(HealthEntries));
感谢您的帮助。
【问题讨论】:
-
是包含要传递给反序列化方法的 JSON 字符串的变量名称 ProfileEntries 吗?
-
是的,我确定它是一样的,否则它会给我一个参数错误
-
您能否在问题中包含完整的错误数据?
标签: c# asp.net json serialization