Json: how to properly remove escape characters using json.net

I have a json answer in the following format.

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

I believe json.net handles escape characters, and I used the code below to deserialize it into a collection of dictionaries.

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

But this json parsing throws the exception "Invalid property identifier character: Path '[0]', line 1, position 2." Can we solve this by manipulating the json response string?

+11
json string c #
source share
5 answers

Try string contentCorrected = contentCorrected.Replace(@"\", ""); before the deserialization process.

+16
source share
  • Before deserializing, delete all characters "\". Use the replace function.

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

  • The Json string is incomplete or not of the type List<Dictionary<string, string>>" . Correct the type you want to convert json. I changed your json a bit and it worked.

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

0
source share

For me, the code below works

 string contentCorrected = contentCorrected.Replace(**@"\""", ""**); 
0
source share

The problem occurs when valid double quotes are used in the response. Removal and / or replacement will not allow this in all cases. This also upset me until I found a simple solution:

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

SHORT ANSWER: first you need to deserialize the escaped string, but not into the target CLR type, but deserialize to another string:

 // 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); 

FULL ANSWER (but interesting) Using string.Replace(... can generate an invalid string because it can damage certain special characters that require proper backslash deserialization.

This type of escaped string is usually generated when a string that was already a json string is serialized again (or even more times). This causes something like "different levels of serialization" (actually it is a serialization of a string with reserved characters), and the result is backslash characters (or groups of one, two or more backslashes follow: \, \\, \\\) are scattered throughout the line. Thus, to properly remove them, it is not enough to replace them with empty ones.

CORRECT WAY: The best way to get an unescaped string is to do the first deserialization for the string type (repeat this several times if necessary) and then do the final deserialization for the target CLR type:

 // -- 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); 
0
source share

All Articles