我从公共 API 接收到一个 JSON 对象,该对象的属性本身就是一个转义的 JSON 字符串。
{
"responses":[
{
"info":"keep \"this\" in a string",
"body":"{\"error\":{\"message\":\"Invalid command\",\"type\":\"Exception\",\"code\":123}}"
},
{
"info":"more \"data\" to keep in a string",
"body":"{\"error\":{\"message\":\"Other error\",\"type\":\"Exception\",\"code\":321}}"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如何将此属性转换为实际的 JSON 对象(未转义),以便使用 NewtonSoft Json.NET 反序列化整个响应?
这是我根据Sam I am的回答使用的可行解决方案:
dynamic obj = JsonConvert.DeserializeObject(json);
foreach (var response in (IEnumerable<dynamic>)obj.responses)
{
response.body = JsonConvert.DeserializeObject((string)response.body);
}
string result = JsonConvert.SerializeObject(obj);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6196 次 |
| 最近记录: |