我正在編寫使用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 + CmVuZG9iagozIDAgb2JqC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RB 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
}
添加「BatchPrintBatchInfo」類的代碼 – Gusman
@Gusman - 添加了BatchPrintBatchInfo。 – Chris
好吧,你確定它是一個字節數組的ArrayList,而不是一個字節的ArrayList? – Gusman