2017-05-05 47 views
0

我正在編寫使用Web API(Web API 2)獲取數據的控制檯應用程序。控制檯應用程序將一個複雜的對象傳遞給web api(到目前爲止沒有問題)。我使用Newtonsoft json來序列化/反序列化數據。問題在於,當我在控制檯應用程序中反序列化返回數據時,它將無法使用。下面是數據退還─使用Newtonsoft不工作的De-Serializing Json

[\「JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjM2MzYtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4 + CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4 + CmVuZG9iagozIDAgb2JqCjw8Ci9UeXBlL1BhZ2VzCi9Db3VudCAxCi9LaWRzWzcgMCBSXQo + PgplbmRvYmoKNCAwIG9iagpbCi9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSQpdCmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlL0ZvbnQKL1N1YnR5cGUvVHlwZTEKL0Jhc2VGb250L0hlbHZldGljYQovRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nCj4 + CmVuZG9iago2IDAgb2JqCjw8Ci9MZW5ndGggMTM4Ci9GaWx0ZXIvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtClgJ4 + UqVDA30jMAEgomZhZAhpkJiCxKVQhXyOMFyhoZWOpZmimYGVromRopgJUYGekZG8GVOIUo6LsZKxgaKISkKRiAYVG6gompkZ65oYVCiA/EBJBeY0M9oECKgoZjmKOCpkJIlkKIloJrCClGGBnomZmAzX AO8g93cQ1CNiYQjPB7KBAAubYyZAplbmRzdHJlYW0KZW5kb2JqCjcgMCBvYmoKPDwKL1BhcmVudCAzIDAgUgovVHlwZS9QYWdlCi9NZWRpYUJveFswIDAgNjEyIDc5Ml0KL1Jlc291cmNlcwo8PAovUHJvY1NldCA0IDAgUgovWE9iamVjdAo8PAo + PgovRm9udAo8PAovRjMgNSAwIFIKPj4KPj4KL0NvbnRlbnRzIDYgMCBSCi9Hcm91cAo8PAovQ1MvRGV2aWNlUkdCCi9TL1RyYW5zcGFyZW5jeQo + PGO + PgplbmRvYmoKeHJlZgowIDgKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAxNzYgMDAwMDAgbiAKMDAwMDAwMDIyNCAwMDAwMCBuIAowMDAwMDAwMjc5IDAwMDAwIG4gCjAwMDAwMDAzMjkgMDAwMDAgbiAKMDAwMDAwMDQyMiAwMDAwMCBuIAowMDAwMDAwNjMxIDAwMDAwIG4gCnRyYWlsZXIKPDwKL0lEWzwwNUY3MTFDRjlGRjFDRTRFQTUwNjIxMUNGNzhEQzRBRT48MDVGNzExQ0Y5RkYxQ0U0RUE1MDYyMTFDRjc4REM0QUU + XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4 + CnN0YXJ0eHJlZgo4MjgKJSVFT0YK \「]

返回它被反序列化後應字節數組的ArrayList數據。但是替代地在試圖deserialize-

類型 「Newtonsoft.Json.JsonSerializationException」的未處理的異常產生的下面的錯誤發生在 Newtonsoft.Json.dll

其他信息:誤差變換值 「[」 + JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjQ0NTQtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4 + CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4 + CmVuZG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UM2RDc2QTJEMjRB OTI3OEY0Q0ZDNTExQjI4RD48QzYzRUFDNkQ3NkEyRDI0QTkyNzhGNENGQzUxMUIyOEQ + XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4 + CnN0YXJ0eHJlZgo4MjgKJSVFT0YK 「]」 鍵入 'System.Collections.ArrayList'。路徑'',第1行,位置 1524。

下面是代碼 - WEB API -

public class BatchController : ApiController 
{ 
    [ActionName("DoBatchWork")] 
    [HttpPost] 
    public string DoBatchWork([FromBody]JObject jWork) 
    { 
     JObject s = JObject.Parse(jWork.ToString()); 

     BatchPrintBatchInfo objWork = JsonConvert.DeserializeObject<BatchPrintBatchInfo>(s.ToString()); 
     ArrayList aryWork = DocumentByBatchReports.CreateBatchReport(objWork); 
     string a = JsonConvert.SerializeObject(aryWork); 
     return a; 

    } 

} 

WEB求職

public static ArrayList DoBatchWork(BatchPrintBatchInfo Work) 
    { 
     HttpClient client = new HttpClient(); 

     var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(Work), Encoding.UTF8, "application/json"); 
     string result = SendRequest("Batch/DoBatchWork", content, HttpMethod.Post); 

     //******FAILS HERE************* 
     ArrayList deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<ArrayList>(result); 


     return deserialized; 
    } 

    public static string SendRequest(string requestUrl, StringContent content, HttpMethod httpMethod) 
    { 
     var client = new HttpClient { BaseAddress = new Uri(serviceUrl) }; 
     var uri = new Uri(serviceUrl + requestUrl); // http://localhost/api/test 

     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     HttpResponseMessage response; 
     response = client.PostAsync(uri, content).Result; 

     if (!response.IsSuccessStatusCode) 
     { 
      throw new ApplicationException(response.ToString()); 
     } 

     string stringResult = response.Content.ReadAsStringAsync().Result; 
     return stringResult; 
    } 

編輯 BATCHPRINTBATCHINFO代碼 -

  using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace RENO_BatchPrintGenerator.PropertyClasses 
{ 
    #region "BatchPrintBatchInfo" 
    [Serializable()] 
    public class BatchPrintBatchInfo 
    { 

     // local property declarations 
     int _id; 
     int _renoBatchPrintCategoryID; 
     Nullable<DateTime> _createdDate; 
     Nullable<DateTime> _deletedDate; 
     string _batchType; 
     int _printCount; 
     DateTime _requestDate; 
     Nullable<DateTime> _lastPrintDate; 
     Nullable<DateTime> _lastLabelsPrintDate; 
     int _userId; 
     string _userFullname; 
     string _filename; 
     string _filenameLabels; 

     bool _notifyUser; 
     #region "Constructors" 
     public BatchPrintBatchInfo() 
     { 
     } 

     public BatchPrintBatchInfo(int renoBatchPrintCategoryID, DateTime createdDate, string batchType, int printCount, DateTime requestDate, int userId, DateTime deletedDate) 
     { 
      this.ID = 0; 
      this.RenoBatchPrintCategoryID = renoBatchPrintCategoryID; 
      this.CreatedDate = createdDate; 
      this.DeletedDate = deletedDate; 
      this.BatchType = batchType; 
      this.PrintCount = printCount; 
      this.RequestDate = requestDate; 
      this.LastPrintDate = null; 
      this.LastLabelsPrintDate = null; 
      this.UserID = userId; 
      this.UserFullname = ""; 
      this.Filename = ""; 
      this.FilenameLabels = ""; 
      this.NotifyUser = false; 
     } 
     #endregion 

     #region "Public Properties" 
     public int ID 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     public int RenoBatchPrintCategoryID 
     { 
      get { return _renoBatchPrintCategoryID; } 
      set { _renoBatchPrintCategoryID = value; } 
     } 

     public Nullable<DateTime> CreatedDate 
     { 
      get { return _createdDate; } 
      set { _createdDate = value; } 
     } 

     public Nullable<DateTime> DeletedDate 
     { 
      get { return _deletedDate; } 
      set { _deletedDate = value; } 
     } 

     public string BatchType 
     { 
      get { return _batchType; } 
      set { _batchType = value; } 
     } 

     public int PrintCount 
     { 
      get { return _printCount; } 
      set { _printCount = value; } 
     } 

     public DateTime RequestDate 
     { 
      get { return _requestDate; } 
      set { _requestDate = value; } 
     } 

     public Nullable <DateTime> LastPrintDate 
     { 
      get { return _lastPrintDate; } 
      set { _lastPrintDate = value; } 
     } 

     public Nullable<DateTime> LastLabelsPrintDate 
     { 
      get { return _lastLabelsPrintDate; } 
      set { _lastLabelsPrintDate = value; } 
     } 

     public string UserFullname 
     { 
      get { return _userFullname; } 
      set { _userFullname = value; } 
     } 

     public int UserID 
     { 
      get { return _userId; } 
      set { _userId = value; } 
     } 

     public string Filename 
     { 
      get { return _filename; } 
      set { _filename = value; } 
     } 

     public string FilenameLabels 
     { 
      get { return _filenameLabels; } 
      set { _filenameLabels = value; } 
     } 

     public bool NotifyUser 
     { 
      get { return _notifyUser; } 
      set { _notifyUser = value; } 
     } 
     #endregion 
    } 
    #endregion 
} 
+0

添加「BatchPrintBatchInfo」類的代碼 – Gusman

+0

@Gusman - 添加了BatchPrintBatchInfo。 – Chris

+0

好吧,你確定它是一個字節數組的ArrayList,而不是一個字節的ArrayList? – Gusman

回答

1

你們看到的是一個字節數組,任何IEnumerable<byte>將序列爲Base64。但ArrayList不是IEnumerable<byte>IEnumerable然後解串器不知道如何處理Base64的數據做的ArrayList內容沒有定義(這是object)。如果你把它反序列化到byte[]或到List<byte>它會工作。

每例如:

byte[] deserialized = JsonConvert.DeserializeObject<byte[]>(result); 

此外,避免儘可能多的,你可以ArrayList,使用真正的數組或泛型集合像List<T>

編輯:你真的有一個字節一個ArrayList []內,我的錯,那麼正確的反序列化的代碼應該是:

List<byte[]> deserialized = JsonConvert.DeserializeObject<List<byte[]>>(result); 

這與您的數據工作的全面測試:

string data = "[\"JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjM2MzYtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2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CnRyYWlsZXIKPDwKL0lEWzwwNUY3MTFDRjlGRjFDRTRFQTUwNjIxMUNGNzhEQzRBRT48MDVGNzExQ0Y5RkYxQ0U0RUE1MDYyMTFDRjc4REM0QUU+XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4+CnN0YXJ0eHJlZgo4MjgKJSVFT0YK\"]"; 

List<byte[]> datdes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<byte[]>>(data); 
2

你看到的是一個base64編碼形式在,如果你嘗試解碼它,你會看到一個PDF文件。

最有可能的BatchPrintBatchInfo Work參數包含了打印輸出爲PDF

+0

我沒有嘗試Base64Decode方法,並在解碼之前刪除了json字符。我確實收到了部分可讀的文本,並且似乎表示PDF就像你說的那樣。但是,想用一個返回字節數組的數組列表的方法來處理這個問題。 – Chris

+0

什麼字節數組列表? – Max

+0

SSRS報告轉換成字節數組,以PDF的形式寫入NAS驅動器。 – Chris