Why cant Newtonsoft.Json deserialize a Dictionary list

ぃ、小莉子 提交于 2019-12-12 06:44:33

问题


nvlst declared here then populated

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

When the service is finished, it returns

JsonConvert.SerializeObject(nvlst, Formatting.Indented)

When the client attempts to deserialize, it throws an error

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

here is the error message:

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.

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

回答1:


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


来源:https://stackoverflow.com/questions/20055978/why-cant-newtonsoft-json-deserialize-a-dictionary-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!