6
以“ TryParse”方式反序列化json
当我向服务(我不拥有)发送请求时,它可能会以请求的JSON数据或如下所示的错误进行响应: { "error": { "status": "error message", "code": "999" } } 在这两种情况下,HTTP响应代码均为200 OK,因此我无法使用该代码来确定是否存在错误-我必须反序列化响应以进行检查。所以我有这样的东西: bool TryParseResponseToError(string jsonResponse, out Error error) { // Check expected error keywords presence // before try clause to avoid catch performance drawbacks if (jsonResponse.Contains("error") && jsonResponse.Contains("status") && jsonResponse.Contains("code")) { try { error = new JsonSerializer<Error>().DeserializeFromString(jsonResponse); return true; …