我有一个这样的字符串:
"[
{
\"SomeProperty\": 22
}
]"
我正在尝试将其反序列化为已知类型的列表:
string toProcess = $@"[{text}]".Replace("\n", ",");
toProcess = Regex.Unescape(toProcess);
List<KnownType> objectList = JsonConvert.DeserializeObject<List<KnownType>>(toProcess);
但是我发现它正在尝试反序列化包含\"
个字符的字符串,并且它在Cannot deserialize the current JSON object
失败。如何正确反序列化?
答案 0 :(得分:-1)
您不需要string toProcess = $@"[{text}]".Replace("\n", ",");
string text = @"[
{
\""SomeProperty\"": 22
}
]";
text = Regex.Unescape(text);
var objectList = JsonConvert.DeserializeObject<List<KnownType>>(text);
Console.WriteLine(objectList[0].SomeProperty);//22
此代码按预期工作。