![](/img/trans.png)
[英]Getting error Type 'System.String' is not supported for deserialization of an array
[英]Type 'System.String' is not supported for deserialization of an array
我在使用 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 Edit添加的方法名称的列表
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反序列化的类(无关的步骤来解决此问题),但将参数从Web服务“保存”功能的单个字符串更改为字符串数组
public void SaveHealthResponse(string[] profilesJson)
{
var healthInfoProfileEntries = new List<HealthProfileEntry>();
try
{
foreach (var profileJson in profilesJson)
{
var temp = JsonConvert.DeserializeObject<HealthProfileEntry>(profileJson);
healthInfoProfileEntries.Add(temp);
}
} ...
这是在我的客户端上构建JSON数组的方式(使用剔除,但可以用常规javascript轻松替换):
ko.utils.arrayForEach(selectedItems, function (item) {
serializedHealthInfo.push(ko.toJSON(item));
});
var dataJson = JSON.stringify({ profilesJson: serializedHealthInfo});
$.ajax({
data: dataJson,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: self.saveUrl
})
数组的反序列化不支持类型System.String
。 我得到了类似的上述错误。 解决了以下问题:
JavaScriptSerializer jss = new JavaScriptSerializer();
MainCentre = jss.Deserialize<List<Centre>>(json);
中心 - 带有子类的类。 如果上述类没有子类,反序列化效果很好。 为了用子类解析类,我在类中添加了子类作为数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.