我编写了一个使用Web API(web api 2)来获取数据的控制台应用程序。控制台应用程序将一个复杂的对象传递给web api(到目前为止没问题)。我正在使用Newtonsoft json来序列化/反序列化数据。问题是,当我在控制台应用程序中反序列化返回数据时,它返回无法使用。以下是返回的数据 -
[\" JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjM2MzYtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4 + CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4 + CmVuZG9iagozIDAgb2JqCjw8Ci9UeXBlL1BhZ2VzCi9Db3VudCAxCi9LaWRzWzcgMCBSXQo + PgplbmRvYmoKNCAwIG9iagpbCi9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSQpdCmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlL0ZvbnQKL1N1YnR5cGUvVHlwZTEKL0Jhc2VGb250L0hlbHZldGljYQovRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nCj4 + CmVuZG9iago2IDAgb2JqCjw8Ci9MZW5ndGggMTM4Ci9GaWx0ZXIvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtClgJ4 + UqVDA30jMAEgomZhZAhpkJiCxKVQhXyOMFyhoZWOpZmimYGVromRopgJUYGekZG8GVOIUo6LsZKxgaKISkKRiAYVG6gompkZ65oYVCiA / EBJBeY0M9oECKgoZjmKOCpkJIlkKIloJrCClGGBnomZmAzXAO8g93cQ1CNiYQjPB7KBAAubYyZAplbmRzdHJlYW0KZW5kb2JqCjcgMCBvYmoKPDwKL1BhcmVudCAzIDAgUgovVHlwZS9QYWdlCi9NZWRpYUJveFswIDAgNjEyIDc5Ml0KL1Jlc291cmNlcwo8PAovUHJvY1NldCA0IDAgUgovWE9iamVjdAo8PAo + PgovRm9udAo8PA ovRjMgNSAwIFIKPj4KPj4KL0NvbnRlbnRzIDYgMCBSCi9Hcm91cAo8PAovQ1MvRGV2aWNlUkdCCi9TL1RyYW5zcGFyZW5jeQo + PGO + PgplbmRvYmoKeHJlZgowIDgKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAxNzYgMDAwMDAgbiAKMDAwMDAwMDIyNCAwMDAwMCBuIAowMDAwMDAwMjc5IDAwMDAwIG4gCjAwMDAwMDAzMjkgMDAwMDAgbiAKMDAwMDAwMDQyMiAwMDAwMCBuIAowMDAwMDAwNjMxIDAwMDAwIG4gCnRyYWlsZXIKPDwKL0lEWzwwNUY3MTFDRjlGRjFDRTRFQTUwNjIxMUNGNzhEQzRBRT48MDVGNzExQ0Y5RkYxQ0U0RUE1MDYyMTFDRjc4REM0QUU + XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4 + CnN0YXJ0eHJlZgo4MjgKJSVFT0YK \"]
返回的数据在反序列化后应该是字节数组的arraylist。但是,在尝试反序列化时会生成以下错误 -
未处理的类型异常 ' Newtonsoft.Json.JsonSerializationException'发生在 Newtonsoft.Json.dll
其他信息:转换值时出错 " [" JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjQ0NTQtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4 + CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4 + CmVuZG9iagozIDAgb2JqCjw8Ci9UeXBlL1BhZ2VzCi9Db3Vud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YzRUFDNkQ3NkEyRDI0QTkyNzhGNENGQzUxMUIyOEQ + 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 JOB -
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;
}
EDIT 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 :(得分:2)
您看到的是base64编码格式,如果您尝试对其进行解码,则会看到PDF文件。
BatchPrintBatchInfo Work
参数最有可能包含PDF格式的打印输出
答案 1 :(得分:1)
您看到的是一个字节数组,任何IEnumerable<byte>
都将被序列化为Base64。但由于ArrayList
不是IEnumerable<byte>
而是IEnumerable
,因此解串器不知道如何处理Base64数据,因为ArrayList
内容未定义(object
})。如果您将其反序列化为byte[]
或List<byte>
,则会有效。
每个例子:
byte[] deserialized = JsonConvert.DeserializeObject<byte[]>(result);
另外,尽可能避免ArrayList
使用真实数组或通用集合,例如List<T>
List<byte[]> deserialized = JsonConvert.DeserializeObject<List<byte[]>>(result);
这是使用您的数据的完整测试:
string data = "[\"JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjM2MzYtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4+CmVuZG9iagozIDAgb2JqCjw8Ci9UeXBlL1BhZ2VzCi9Db3VudCAxCi9LaWRzWzcgMCBSXQo+PgplbmRvYmoKNCAwIG9iagpbCi9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSQpdCmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlL0ZvbnQKL1N1YnR5cGUvVHlwZTEKL0Jhc2VGb250L0hlbHZldGljYQovRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nCj4+CmVuZG9iago2IDAgb2JqCjw8Ci9MZW5ndGggMTM4Ci9GaWx0ZXIvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtClgJ4+UqVDA30jMAEgomZhZAhpkJiCxKVQhXyOMFyhoZWOpZmimYGVromRopgJUYGekZG8G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