2017-02-28 94 views
0

我希望这是非常简单的东西,必须是其他开发人员的常见场景。错误,同时反序列化字符串中的转义字符

我从SQL发送一个包含错误细节的OUT变量。在我的数据层API,我检查它在VS调试器,这就是它如何出现:

var string_from_stored_proc = "[{\"errornumber\":2627,\"errormessage\":\"Violation of UNIQUE KEY constraint 'KEY_Subject'. Cannot insert duplicate key in object 'edu.Subject'. The duplicate key value is (MATS10, 2).\",\"errorstate\":1,\"errorseverity\":14,\"errorline\":36,\"errroprocedure\":\"SPCreateSubject\",\"xactstate\":1}]" 

我明白VS调试器增加了逃生()的字符串。当我尝试反序列化使用此字符串:

JsonConvert.DeserializeObject<my_error_object>(string_from_stored_proc); 

我得到这个错误:

{"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'BaaS.DTO.SQLErrorHandler' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."}

json2csharp.com没有这个JSON转换。如果我删除所有反斜杠,它会完成这项工作。下面是my_error_object(或json2sharp转换)类:

public class my_error_object 
{ 
    public int errornumber { get; set; } 
    public string errormessage { get; set; } 
    public int errorstate { get; set; } 
    public int errorseverity { get; set; } 
    public int errorline { get; set; } 
    public string errroprocedure { get; set; } 
    public int xactstate { get; set; } 
} 

我尝试处理string_from_stored_proc,并删除所有反斜杠,但我不能删除转义字符(我认为这是预期的行为)。对于这些斜杠,我无法获得字符串反序列化。

解决方法: 如果我使用var outputJson = Newtonsoft.Json.Linq.JToken.Parse(string_from_stored_proc);,它提供了一个输出以下的输出:

{[ 
    { 
    "errornumber": 2627, 
    "errormessage": "Violation of UNIQUE KEY constraint 'KEY_Subject'. Cannot insert duplicate key in object 'edu.Subject'. The duplicate key value is (MATS10, 2).", 
    "errorstate": 1, 
    "errorseverity": 14, 
    "errorline": 36, 
    "errroprocedure": "SPCreateSubject", 
    "xactstate": 1 
    } 
]} 

在移除开始和结尾的括号,我能够使用NewtonSoft反序列化。

然而,当我使用BadRequest(deserialized_string_from_stored_proc) 从数据层发送和使用

var output = await response.Content.ReadAsAsync<SQLErrorHandler>(); 

输出变量在BusinessLayer检索它再次有很多额外的字符,然后我需要再次处理的。对我来说唯一的方法是使用HttpResults.OK(对象)发送反序列化的对象,并在我的BusinessLayer中读取它。我不想这样做,因为它在错误的情况下不是正确的返回类型。

我在做对吧?我有两个问题:

  1. 有没有办法来反序列化给定的字符串,并寄过来使用 到BusinessLayer没有字符串处理呢?
  2. 如果第一个不可行,我可以简单地将字符串传递给 BusinessLayer并在那里反序列化它(首选方法)?

我很高兴提供更多信息或代码段来重现错误。

编辑: per @ Equalsk的建议,我已经能够正确地反序列化它。但是,它目前在Datalayer中。这里是输出:

var equalskObj = JsonConvert.DeserializeObject<List<SQLErrorHandler>>(outputFromSP.Value.ToString()); 
return Ok(equalskObj); 

但是,我仍然无法做到首选的方式(#2)。如果我使用return BadRequest(string_from_stored_proc)字符串发送到BusinessLayer发送

"{\r\n \"message\": \"[{\\"errornumber\\":2627,\\"errormessage\\":\\"Violation of UNIQUE KEY constraint 'KEY_Subject'. Cannot insert duplicate key in object 'edu.Subject'. The duplicate key value is (MATS10, 2).\\",\\"errorstate\\":1,\\"errorseverity\\":14,\\"errorline\\":36,\\"errroprocedure\\":\\"SPCreateSubject\\",\\"xactstate\\":1}]\"\r\n}"

当我还是要处理它。有没有更清晰的方式发送它,然后按照建议进行转换?我想我应该为此发布一个简化的问题,因为这些是单独的问题。我真的很感兴趣,看看最好的办法是什么。

+1

你试过'JsonConvert.DeserializeObject <名单>(string_from_stored_proc);'? – Equalsk

+2

它正在寻找一个列表/数组类型,因为你的SQL字符串在其内容周围具有'['和']'。删除这些,然后反序列化。 –

+0

@Equalsk - 非常感谢。它做了诡计。我以某种方式忽略它即使在存储过程中设置的返回变量是varchar(C#模型中的字符串)时也返回一个列表。 – sandiejat

回答

1

我被困在一个类似的东西。为了让您的DL输出,可以如下返回:

,自DL(被叫/秒)API

return Content(<HttpStatusCode>, "Custom Error Message Here"); 

例如:

Return Content(System.Net.HttpStatusCode.BadRequest, e.Message.ToString()); 
现在

在BL(头等舱/呼叫)API,如果StatusCode不是成功的,则读取响应结果。就像这样:

if(!response.IsSuccessStatusCode) 
{ 
    var errorOutput = response.Content.ReadAsStringAsync().Result; 
    //Now send this errorOutput from this API to the client. Return an HTTPResult or another Content from here. Upto you! 
} 
return Ok(); //If response.statuscode is Success 
+0

这很有帮助。通过内容更好,因为它支持自定义错误/消息以及状态代码。 – sandiejat