为什么Newtonsoft.Json不能反序列化字典列表 [英] Why cant Newtonsoft.Json deserialize a Dictionary list

查看:112
本文介绍了为什么Newtonsoft.Json不能反序列化字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此声明的nvlst然后填充

nvlst declared here then populated

Dictionary<string, string> nvlst = new Dictionary<string, string>();

服务完成后,它会返回

JsonConvert.SerializeObject(nvlst, Formatting.Indented)

客户端尝试反序列化时,会引发错误

When the client attempts to deserialize, it throws an error

alst = JsonConvert.DeserializeObject<Dictionary<string, string>>(sb.ToString());

这是错误消息:

Error converting value "{
  "Status": "SUCCESS:",
  "CustomerId": "1",
  "LicenseKey": "03bad945-37c0-4fa0-8126-fb4935df396d",
  "MachineFingerPrint": "9B82-A8ED-3DE9-0D8C-26DD-2D83-C17F-C2E3",
  "ExpirationDate": "11/18/2014",
  "LicenseDate": "11/18/2013",
  "LicenseGraceDay": "7",
  "RequireRenewal": "Y",
  "BaseUrl": "http://www.xxx-xxxxxxx.com",
  "HelpUrl": "http://www.xxx-xxxxxxx.com/help",
  "APIUrl": "http://localhost:63583/api/Ajax/runGenericMethodFromApp",
  "CoName": "TestCustomer1",
  "Addr1": "123 Any Street",
  "Addr2": "",
  "City": "Louisville",
  "State": "KY",
  "Zip": "40245"
}" to type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'. Path '', line 1, position 704.

sb.ToString()的实际值低于

the actual value of the sb.ToString() is below

"\"{\\r\\n  \\\"Status\\\": \\\"SUCCESS:\\\",\\r\\n  \\\"CustomerId\\\": \\\"1\\\",\\r\\n  \\\"LicenseKey\\\": \\\"03bad945-37c0-4fa0-8126-fb4935df396d\\\",\\r\\n  \\\"MachineFingerPrint\\\": \\\"9B82-A8ED-3DE9-0D8C-26DD-2D83-C17F-C2E3\\\",\\r\\n  \\\"ExpirationDate\\\": \\\"11/18/2014\\\",\\r\\n  \\\"LicenseDate\\\": \\\"11/18/2013\\\",\\r\\n  \\\"LicenseGraceDay\\\": \\\"7\\\",\\r\\n  \\\"RequireRenewal\\\": \\\"Y\\\",\\r\\n  \\\"BaseUrl\\\": \\\"http://www.xxx-xxxxxxx.com\\\",\\r\\n  \\\"HelpUrl\\\": \\\"http://www.xxx-xxxxxxx.com/help\\\",\\r\\n  \\\"APIUrl\\\": \\\"http://localhost:63583/api/Ajax/runGenericMethodFromApp\\\",\\r\\n  \\\"CoName\\\": \\\"TestCustomer1\\\",\\r\\n  \\\"Addr1\\\": \\\"123 Any Street\\\",\\r\\n  \\\"Addr2\\\": \\\"\\\",\\r\\n  \\\"City\\\": \\\"Louisville\\\",\\r\\n  \\\"State\\\": \\\"KY\\\",\\r\\n  \\\"Zip\\\": \\\"40245\\\"\\r\\n}\""

推荐答案

您的字符串看起来像是双序列化的,所以这就是为什么它不能正确反序列化的原因.如果您使用的是WebAPI之类的框架,它将为您处理序列化,因此您无需调用JsonConvert.SerializeObject().相反,请更改ApiController方法以直接返回对象(请注意,您还需要更改方法签名).例如:

It looks like your string is getting double-serialized, so that is why it is not deserializing properly. If you are using a framework like WebAPI, it handles serialization for you, so you don't need to call JsonConvert.SerializeObject(). Instead, change your ApiController method to return the object directly (note that you'll need to change the method signature as well). For example:

public class FooController : ApiController
{
    [HttpPost]
    public Dictionary<string, string> runGenericMethodFromApp()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("Status", "SUCCESS,");
        dict.Add("CustomerId", "1");
        dict.Add("LicenseKey", "03bad945-37c0-4fa0-8126-fb4935df396d");
        dict.Add("MachineFingerPrint", "9B82-A8ED-3DE9-0D8C-26DD-2D83-C17F-C2E3");
        dict.Add("ExpirationDate", "11/18/2014");
        dict.Add("LicenseDate", "11/18/2013");
        dict.Add("LicenseGraceDay", "7");
        dict.Add("RequireRenewal", "Y");
        dict.Add("BaseUrl", "http://www.xxx-xxxxxxx.com");
        dict.Add("HelpUrl", "http://www.xxx-xxxxxxx.com/help");
        dict.Add("APIUrl", "http://localhost:63583/api/Ajax/runGenericMethodFromApp");
        dict.Add("CoName", "TestCustomer1");
        dict.Add("Addr1", "123 Any Street");
        dict.Add("Addr2", "");
        dict.Add("City", "Louisville");
        dict.Add("State", "KY");
        dict.Add("Zip", "40245");
        return dict;
    }
}

这篇关于为什么Newtonsoft.Json不能反序列化字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆