输入' System.String'不支持反序列化数组

时间:2014-04-17 20:56:01

标签: c# asp.net json serialization

我遇到了使用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"

}

它发送给Web服务的JSON最终看起来像这样:

{
    "ProfileEntries": [
        {
            "TypeId": "C",
            "CodeId": "HEARTDZ ",
            "StudComment": "testComment"
        },
        {
            "TypeId": "C",
            "CodeId": "HIBLOOD "
        },
        {
            "TypeId": "F",
            "CodeId": "MARFANF ",
            "StudComment": "testComment"
        },
        {
            "TypeId": "F",
            "CodeId": "MITRALF "
        }
    ]
}

你看到一些没有“StudComment”的对象的原因是因为我使用knockout来保存一个在加载时原本不属于对象的值。

我的网络服务基本上是为了获取反序列化的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));

感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

我想通了,以为我会分享参考。我最终摆脱了用于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数组在我的客户端构建的方式(使用knockout,但可以使用常规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
 })

答案 1 :(得分:0)

数组的反序列化不支持类型 System.String。 我得到了类似的上述错误。解决了以下问题:

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

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