【问题标题】:Type 'System.String' is not supported for deserialization of an array数组的反序列化不支持类型“System.String”
【发布时间】: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


【解决方案1】:

我想通了,我想我会分享以供参考。我最终摆脱了用于 JSON 反序列化的类(修复此问题的无关步骤),但将参数从单个字符串更改为 array Web 服务“Save”函数的字符串到此 p>

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
 })

【讨论】:

    【解决方案2】:

    类型 System.String 不支持数组的反序列化。 我遇到了类似的上述错误。解决方法如下:

    JavaScriptSerializer jss = new JavaScriptSerializer();
    MainCentre = jss.Deserialize<List<Centre>>(json);
    

    Centre - 带有子类的类。 如果上面的类没有子类,反序列化效果很好。为了用子类解析类,我将子类作为数组添加到类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2014-10-28
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多