小编典典

如何在JSON对象中转换转义的JSON字符串?

json

我从公共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}}"
      }
   ]
}

我如何将此属性转换为实际的JSON对象(未转义),以便使用NewtonSoft Json.NET反序列化整个响应?


阅读 962

收藏
2020-07-27

共1个答案

小编典典

这是我根据 Sam 的答案使用的可行解决方案:

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);
2020-07-27