Json:如何正确地使用json.net删除转义字符

时间:2021-03-28 00:26:01

I have json response in the below format.

我有以下格式的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\\\":\\\"\\\"}]

I believe json.net handles the escape characters and I used the below code to deserialize it to a dictionary collection.

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

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

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

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

4 个解决方案

#1


16  

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

尝试字符串contentmodify = content纠正。取代(@ " \ "," ");在反序列化过程。

#2


0  

  1. Remove all the "\" character before you deserialize it. Use replace function.

    在反序列化之前,删除所有“\”字符。使用替换功能。

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

    yourJsonString。替换(“\ \ \ \ \ "," ");

  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.

    您的Json字符串是不完整的,或者看起来不是类型列表 >"。更正要转换json的类型。我对json进行了如下修改,它是有效的。

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

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

#3


0  

For me the code below works

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

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

#4


0  

The problem occurs when valid double quotes are used within the answer. Removing and/or Replacing won't solved this in all cases. It frustrated me too until I found a simple solution:

当在答案中使用有效的双引号时,就会出现问题。删除和/或替换不会在所有情况下都解决这个问题。直到我找到一个简单的解决办法:

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

#1


16  

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

尝试字符串contentmodify = content纠正。取代(@ " \ "," ");在反序列化过程。

#2


0  

  1. Remove all the "\" character before you deserialize it. Use replace function.

    在反序列化之前,删除所有“\”字符。使用替换功能。

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

    yourJsonString。替换(“\ \ \ \ \ "," ");

  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.

    您的Json字符串是不完整的,或者看起来不是类型列表 >"。更正要转换json的类型。我对json进行了如下修改,它是有效的。

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

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

#3


0  

For me the code below works

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

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

#4


0  

The problem occurs when valid double quotes are used within the answer. Removing and/or Replacing won't solved this in all cases. It frustrated me too until I found a simple solution:

当在答案中使用有效的双引号时,就会出现问题。删除和/或替换不会在所有情况下都解决这个问题。直到我找到一个简单的解决办法:

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