試圖通過兩個模塊之間的這種對象的:傳遞JSON,反序列化失敗,因爲backslashs
public class Transaction
{
public string ID { get; set; }
public string ErrorMessage { get; set; }
public int ErrorCode { get; set; }
}
序列化:
transaction.ErrorMessage = "temptemptemp";
transaction.ErrorCode = 123;
transaction.ID = "0";
return Ok(JsonConvert.SerializeObject(transaction));
反序列化:
using (WebClient webClient = new WebClient())
{
string json = webClient.DownloadString(url);
Transaction transaction = JsonConvert.DeserializeObject<Transaction>(json);
}
在調試窗口
,我看到這樣的兩個字符串:
- 序列化之後 - { 「ID」: 「0」, 「的ErrorMessage」: 「temptemptemp」, 「錯誤碼」:123}
- 反序列化之前 - 「{\」 ID \ 「:\」 0 \」, \ 「的ErrorMessage \」:\ 「temptemptemp \」 \ 「錯誤碼\」:123}」
等我發現了以下情況例外:
誤差變換值 「{」 ID 「:」0「,」ErrorMessage「:」temptemptemp「,」ErrorCode「:123}」鍵入'WebService.Transaction'。路徑'',第1行,位置75.
什麼的'好() '方法呢? –
'Ok()'函數返回一個'System.Web.Http.Results.OkNegotiatedContentResult' – toy4fun
這些斜線只是轉義字符,這不應該影響轉換爲JSON – AdamJeffers