18

我有以下格式的 json 响应。

"[{\\\"JobID\\\":\\\"1\\\",\\\"BillGenerationDate\\\":\\\"4/29/2013 2:53:34 PM\\\",\\\"BillID\\\":\\\"115743\\\",\\\"BillNo\\\":\\\"115743\\\",\\\"CustomerID\\\":\\\"4041705\\\",\\\"PayStatus\\\":\\\"0\\\",\\\"PaymentRequiredStatus\\\":\\\"True\\\",\\\"ProductName\\\":\\\"Epic FBO test\\\",\\\"Description\\\":\\\"Epic Automation 2\\\\r\\\\n\\\",\\\"ProductType\\\":\\\"eBill \\\",\\\"DueType\\\":\\\"-1\\\",\\\"DueDate\\\":\\\"2013-03-15\\\",\\\"Amount\\\":\\\"63.70\\\",\\\"Cost\\\":\\\"\\\"},
{\\\"JobID\\\":\\\"9\\\",\\\"BillGenerationDate\\\":\\\"5/2/2013 10:21:39 AM\\\",\\\"BillID\\\":\\\"115743\\\",\\\"BillNo\\\":\\\"115743\\\",\\\"CustomerID\\\":\\\"4041705\\\",\\\"PayStatus\\\":\\\"0\\\",\\\"PaymentRequiredStatus\\\":\\\"True\\\",\\\"ProductName\\\":\\\"FBO Test Product\\\",\\\"Description\\\":\\\"FBO Product Test\\\",\\\"ProductType\\\":\\\"eBill \\\",\\\"DueType\\\":\\\"-1\\\",\\\"DueDate\\\":\\\"2013-05-01\\\",\\\"Amount\\\":\\\"150.70\\\",\\\"Cost\\\":\\\"\\\"}]

我相信 json.net 处理转义字符,我使用下面的代码将其反序列化为字典集合。

var billList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(contentCorrected);

但是这个 json 解析抛出异常“无效的属性标识符字符:。路径'[0]',第 1 行,位置 2。” 我们可以通过操作 json 响应字符串来解决这个问题吗?

4

5 回答 5

20

简短的回答:首先您需要反序列化转义的字符串,但不是目标 CLR 类型,而是反序列化为另一个字符串(必要时重复);然后,将其反序列化为目标类型:

// Initial example json string:  "\"{\\\"Property1\\\":1988,\\\"Property2\\\":\\\"Some data :D\\\"}\""


// First, deserialize to another string (unescaped string).
string unescapedJsonString = JsonConvert.DeserializeObject<string>(escapedJsonString);
Debug.WriteLine(unescapedJsonString);
// Prints:
// "{\"Property1\":1988,\"Property2\":\"Some data :D\"}"


// Second, deserialize to another string, again (in this case is necessary)
var finalUnescapedJsonString = JsonConvert.DeserializeObject<string>(unescapedJsonString);
Debug.WriteLine(finalUnescapedJsonString);
// This time prints a final, unescaped, json string:
// {"Property1":1988,"Property2":"Some data :D"}


// Finally, perform final deserialization to the target type, using the last unescaped string.
MyClass targetObject = JsonConvert.DeserializeObject<MyClass>(finalUnescapedJsonString);

长答案(但有趣) 使用string.Replace(...可能会生成无效字符串,因为它可能会损坏某些需要反斜杠正确反序列化的特殊字符。

这种类型的转义字符串通常是在已经是 json 字符串的字符串再次序列化(甚至更多次)时生成的。这会导致类似“各种级别的序列化”(它实际上是带有保留字符的字符串的序列化),结果是反斜杠字符(或后面的一个、两个或多个反斜杠组:\、\、\\\)散落在弦上。因此,正确删除它们并不足以将它们替换为空。

正确的方法:获得非转义字符串的更好方法是对字符串类型进行第一次反序列化(如有必要,重复几次),然后对目标 CLR 类型进行最终反序列化:

// -- SERIALIZATION --

// Initial object
MyClass originObj = new MyClass { Property1 = 1988, Property2 = "Some data :D" };

// "First level" Of serialization.
string jsonString = JsonConvert.SerializeObject(originObj);
Debug.WriteLine(jsonString);
// Prints: 
// {"Property1":1988,"Property2":"Some data :D"}


// "Second level" of serialization.
string escapedJsonString = JsonConvert.SerializeObject(jsonString);
Debug.WriteLine(escapedJsonString);            
// "{\"Property1\":1988,\"Property2\":\"Some data :D\"}"
// Note the initial and final " character and de backslash characters

// ...
// at this point you could do more serializations ("More levels"), Obtaining as a result more and more backslash followed,
// something like this:
// "\"{\\\"Property1\\\":1988,\\\"Property2\\\":\\\"Some data :D\\\"}\""
// Note that is... very very crazy :D
// ...

// -- DESERIALIZATION --

// First deserialize to another string (unescaped string).
string unescapedJsonString = JsonConvert.DeserializeObject<string>(escapedJsonString);
Debug.WriteLine(unescapedJsonString);
// Prints:
// {"Property1":1988,"Property2":"Some data :D"}

// ...
// at this point you could repeat more deserializations to string, if necessary. For example if you have many backslash \\\
// ...

// Finally, perform final deserialization to the target type, using the last unescaped string.
MyClass targetObject = JsonConvert.DeserializeObject<MyClass>(unescapedJsonString);
于 2019-01-29T17:59:57.837 回答
15

在反序列化过程之前尝试string contentCorrected = contentCorrected.Replace(@"\", "");

于 2013-05-02T08:25:43.793 回答
1
  1. Remove all the "\" character before you deserialize it. Use replace function.

    yourJsonString.Replace("\\\\\", "");

  2. Your Json string is incomplete or doesnot seems to be of type List<Dictionary<string, string>>". Correct the type you want the json to be converted. I modified your json a little as follows and it worked.

    newJson = "{ \"array\":" + yourJsonString + "}"

于 2013-05-02T06:55:05.073 回答
1

在答案中使用有效的双引号时会出现问题。删除和/或更换不会在所有情况下都解决这个问题。在我找到一个简单的解决方案之前,我也很沮丧:

var billList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(@contentCorrected);
于 2017-03-06T06:46:45.297 回答
0

对我来说,下面的代码有效

string contentCorrected = contentCorrected.Replace(**@"\""", ""**);
于 2016-12-13T09:57:50.730 回答