简体   繁体   English

Newtonsoft转义JSON字符串无法解除对象的反序列化

[英]Newtonsoft escaped JSON string unable to deseralize to an object

Question background: 问题背景:

I am receiving a JSON response via a HttpResponseMessage, as shown: 我通过HttpResponseMessage收到JSON响应,如下所示:

var jsonString= response.Content.ReadAsStringAsync().Result;

This is giving me the following simple escaped JSON string result: 这给了我以下简单的转义JSON字符串结果:

"\"{\\\"A\\\":\\\"B\\\"}\""

The problem: 问题:

I am using Newtonsoft to try and deserialize this to a model: 我正在使用Newtonsoft尝试将其反序列化为模型:

SimpleModel simpleModel= JsonConvert.DeserializeObject<SimpleModel>(jsonString);

The Class model of SimpleModel : SimpleModel的Class模型:

 public class SimpleModel
 {
     public string A { set; get; }
 }

The conversion is giving me the following error: 转换给我以下错误:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Error converting value "{"A":"B"}" to type 'PyeWebClient.Tests.ModelConversionTests+SimpleModel'. Path '', line 1, position 15.

The JSON I receive back from the Task Result is valid, so I cannot understand what the problem is to cause the conversion error, what is the correct way to format the JSON string so it can be converted to its C# model type? 我从任务结果收到的JSON是有效的,所以我无法理解导致转换错误的问题是什么,格式化JSON字符串的正确方法是什么,以便将其转换为C#模型类型?

You json appears serialize twice. 你json出现两次serialize

1) So you have to first deserialize into string and then again deserialize into your SimpleModel like 1)所以你必须首先反序列化为字符串,然后再次反序列化为你的SimpleModel

string json = "\"{\\\"A\\\":\\\"B\\\"}\"";

string firstDeserialize = JsonConvert.DeserializeObject<string>(json);

SimpleModel simpleModel = JsonConvert.DeserializeObject<SimpleModel>(firstDeserialize); 

Output: 输出:

在此输入图像描述

2) If you don't want to deserialize twice then parse your json into JToken and then again parse it into JObject like 2)如果你不想反序列化两次然后将你的json解析为JToken ,然后再将它解析成JObject就好了

string json = "\"{\\\"A\\\":\\\"B\\\"}\"";

JToken jToken = JToken.Parse(json);
JObject jObject = JObject.Parse((string)jToken);

SimpleModel simpleModel = jObject.ToObject<SimpleModel>();

Output: 输出:

在此输入图像描述

Question: How it will be serialize twice? 问题:它将如何序列化两次?

Answer: When you return your result from HttpResponseMessage you successfully serialized your result and after reading this result from ReadAsStringAsync then this method again serialize your result that already serialized. 答:当您从HttpResponseMessage返回结果时,您已成功序列化结果,并在从ReadAsStringAsync读取此结果后,此方法再次序列化已经序列化的结果。

you can just unescape the json string back to normal string and than use DeserializeObject 您可以将json字符串转换回普通字符串,而不是使用DeserializeObject

 string jsonString = "\"{\\\"A\\\":\\\"B\\\"}\"";

 jsonString = Regex.Unescape(jsonString); //almost there
 jsonString = jsonString.Remove(jsonString.Length - 1, 1).Remove(0,1); //remove first and last qoutes
 SimpleModel simpleModel = JsonConvert.DeserializeObject<SimpleModel>(jsonString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM