用" \"转换json字符串到c#中的字典

时间:2014-05-07 05:17:51

标签: c# asp.net json dictionary

我想将以下json字符串转换为包含键值对的dictionaly

["\"TransferDate\" : \"05/30/2014\",\"Location\" : \"013\",\"VendorName\" : \"fdgfg\",\"VendorName_Other\" : \"\",\"Add1\" : \"\",\"Add2\" : \"\",\"Add3\" : \"\",\"City\" : \"\",\"State\" : \"\",\"Zip\" : \"\",\"Vphone\" : \"\",\"Vfax\" : \"\",\"Amount\" : \"$0.00\",\"Description\" : \"\",\"Comments\" : \"\",\"RequestBy\" : \"a den\",\"RPhone\" : \"\",\"FullName\" : \"dfgfg\",\"APhone\" : \"\",\"ReturnAddress\" : \"\",\"itemdesc\" : \"\",\"amount\" : \"\",\"account\" : \"\",\"accdesc\" : \"\",\"comments\" : \"\",\"assetno\" : \"\",\"category\" : \"\",\"internalorder\" : \"\",\"uom\" : \"\",\"Mail Check\":\"0\"",""]

请建议我怎么做,我无法弄清楚,因为我上面的字符串是haing" \"其中的角色。

3 个答案:

答案 0 :(得分:2)

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(YourJson);

答案 1 :(得分:2)

您必须使用Json.NET并从软件包管理器控制台

安装nuget软件包

Install-Package Newtonsoft.Json

并在cs文件中编写代码,如下所示

string str = "[" + "\"TransferDate\" : \"05/30/2014\",\"Location\" : \"013\",\"VendorName\" : \"fdgfg\",\"VendorName_Other\" : \"\",\"Add1\" : \"\",\"Add2\" : \"\",\"Add3\" : \"\",\"City\" : \"\",\"State\" : \"\",\"Zip\" : \"\",\"Vphone\" : \"\",\"Vfax\" : \"\",\"Amount\" : \"$0.00\",\"Description\" : \"\",\"Comments\" : \"\",\"RequestBy\" : \"a den\",\"RPhone\" : \"\",\"FullName\" : \"dfgfg\",\"APhone\" : \"\",\"ReturnAddress\" : \"\",\"itemdesc\" : \"\",\"amount\" : \"\",\"account\" : \"\",\"accdesc\" : \"\",\"comments\" : \"\",\"assetno\" : \"\",\"category\" : \"\",\"internalorder\" : \"\",\"uom\" : \"\",\"Mail Check\":\"0\"" + "]";
            string json = str.Trim().Replace("[", "{").Replace("]", "}");
            Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

答案 2 :(得分:0)

首先在JSON字符串的开头和结尾添加closure。你现在就在阵列中了。

现在你的json字符串将是

string str = "{ \"Location\" : \"013\",\"VendorName\" : \"fdgfg\",\"VendorName_Other\" : \"\",\"Add1\" : \"\" }";

我刚刚拿到了你的JSON样本。

现在只需添加以下代码行。

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(str);

这应该适合你。

希望有所帮助