为什么不能让Newtonsoft.Json反序列化一个Dictionary列表

时间:2013-11-18 19:19:22

标签: json.net

此处声明的nvlst然后填充

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

服务完成后,返回

JsonConvert.SerializeObject(nvlst, Formatting.Indented)

当客户端尝试反序列化时,会抛出错误

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()的实际值低于

"\"{\\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}\""

1 个答案:

答案 0 :(得分:0)

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

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