gpt4 book ai didi

c# - 使用 Newtonsoft 反序列化 Json 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:34 24 4
gpt4 key购买 nike

我正在编写一个使用 Web API (web api 2) 获取数据的控制台应用程序。控制台应用程序将一个复杂的对象传递到 web api(到目前为止没有问题)。我正在使用 Newtonsoft json 序列化/反序列化数据。问题是,当我反序列化控制台应用程序中的返回数据时,它无法使用。以下是返回的数据-

[\"JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjM2MzYtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4+CmVuZG9iagozIDAgb2JqCjw8Ci9UeXBlL1BhZ2VzCi9Db3VudCAxCi9LaWRzWzcgMCBSXQo+PgplbmRvYmoKNCAwIG9iagpbCi9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSQpdCmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlL0ZvbnQKL1N1YnR5cGUvVHlwZTEKL0Jhc2VGb250L0hlbHZldGljYQovRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nCj4+CmVuZG9iago2IDAgb2JqCjw8Ci9MZW5ndGggMTM4Ci9GaWx0ZXIvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtClgJ4+UqVDA30jMAEgomZhZAhpkJiCxKVQhXyOMFyhoZWOpZmimYGVromRopgJUYGekZG8GVOIUo6LsZKxgaKISkKRiAYVG6gompkZ65oYVCiA/EBJBeY0M9oECKgoZjmKOCpkJIlkKIloJrCClGGBnomZmAzXAO8g93cQ1CNiYQjPB7KBAAubYyZAplbmRzdHJlYW0KZW5kb2JqCjcgMCBvYmoKPDwKL1BhcmVudCAzIDAgUgovVHlwZS9QYWdlCi9NZWRpYUJveFswIDAgNjEyIDc5Ml0KL1Jlc291cmNlcwo8PAovUHJvY1NldCA0IDAgUgovWE9iamVjdAo8PAo+PgovRm9udAo8PAovRjMgNSAwIFIKPj4KPj4KL0NvbnRlbnRzIDYgMCBSCi9Hcm91cAo8PAovQ1MvRGV2aWNlUkdCCi9TL1RyYW5zcGFyZW5jeQo+Pgo+PgplbmRvYmoKeHJlZgowIDgKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAxNzYgMDAwMDAgbiAKMDAwMDAwMDIyNCAwMDAwMCBuIAowMDAwMDAwMjc5IDAwMDAwIG4gCjAwMDAwMDAzMjkgMDAwMDAgbiAKMDAwMDAwMDQyMiAwMDAwMCBuIAowMDAwMDAwNjMxIDAwMDAwIG4gCnRyYWlsZXIKPDwKL0lEWzwwNUY3MTFDRjlGRjFDRTRFQTUwNjIxMUNGNzhEQzRBRT48MDVGNzExQ0Y5RkYxQ0U0RUE1MDYyMTFDRjc4REM0QUU+XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4+CnN0YXJ0eHJlZgo4MjgKJSVFT0YK\"]

反序列化后返回的数据应该是字节数组的arraylist。但是,在尝试反序列化时会生成以下错误-

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Error converting value "["JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjQ0NTQtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0Nhd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wIG4gCnRyYWlsZXIKPDwKL0lEWzxDNjNFQUM2RDc2QTJEMjRBOTI3OEY0Q0ZDNTExQjI4RD48QzYzRUFDNkQ3NkEyRDI0QTkyNzhGNENGQzUxMUIyOEQ+XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4+CnN0YXJ0eHJlZgo4MjgKJSVFT0YK"]" to type 'System.Collections.ArrayList'. Path '', line 1, position 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;

}

}

网络作业-

    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
}

最佳答案

您看到的是一个字节数组,任意 IEnumerable<byte>将被序列化为 Base64。但作为 ArrayList不是 IEnumerable<byte>但是IEnumerable那么反序列化器不知道如何处理 Base64 数据作为 ArrayList内容未定义(它是 object )。如果将其反序列化为 byte[]List<byte>它会起作用。

例如:

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

此外,尽可能避免ArrayList ,使用真正的数组或通用集合,如 List<T>

编辑:你真的有一个 ArrayList 里面有一个 byte[],我的错,那么正确的反序列化代码应该是:

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

这是使用您的数据进行的完整测试:

string data = "[\"JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA1MDUxMjM2MzYtMDQnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCi9Qcm9kdWNlcihQREZzaGFycCAxLjUwLjQwMDAgXCh3d3cucGRmc2hhcnAuY29tXCkpCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlL0NhdGFsb2cKL1BhZ2VzIDMgMCBSCj4+CmVuZG9iagozIDAgb2JqCjw8Ci9UeXBlL1BhZ2VzCi9Db3VudCAxCi9LaWRzWzcgMCBSXQo+PgplbmRvYmoKNCAwIG9iagpbCi9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSQpdCmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlL0ZvbnQKL1N1YnR5cGUvVHlwZTEKL0Jhc2VGb250L0hlbHZldGljYQovRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nCj4+CmVuZG9iago2IDAgb2JqCjw8Ci9MZW5ndGggMTM4Ci9GaWx0ZXIvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtClgJ4+UqVDA30jMAEgomZhZAhpkJiCxKVQhXyOMFyhoZWOpZmimYGVromRopgJUYGekZG8GVOIUo6LsZKxgaKISkKRiAYVG6gompkZ65oYVCiA/EBJBeY0M9oECKgoZjmKOCpkJIlkKIloJrCClGGBnomZmAzXAO8g93cQ1CNiYQjPB7KBAAubYyZAplbmRzdHJlYW0KZW5kb2JqCjcgMCBvYmoKPDwKL1BhcmVudCAzIDAgUgovVHlwZS9QYWdlCi9NZWRpYUJveFswIDAgNjEyIDc5Ml0KL1Jlc291cmNlcwo8PAovUHJvY1NldCA0IDAgUgovWE9iamVjdAo8PAo+PgovRm9udAo8PAovRjMgNSAwIFIKPj4KPj4KL0NvbnRlbnRzIDYgMCBSCi9Hcm91cAo8PAovQ1MvRGV2aWNlUkdCCi9TL1RyYW5zcGFyZW5jeQo+Pgo+PgplbmRvYmoKeHJlZgowIDgKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAxNzYgMDAwMDAgbiAKMDAwMDAwMDIyNCAwMDAwMCBuIAowMDAwMDAwMjc5IDAwMDAwIG4gCjAwMDAwMDAzMjkgMDAwMDAgbiAKMDAwMDAwMDQyMiAwMDAwMCBuIAowMDAwMDAwNjMxIDAwMDAwIG4gCnRyYWlsZXIKPDwKL0lEWzwwNUY3MTFDRjlGRjFDRTRFQTUwNjIxMUNGNzhEQzRBRT48MDVGNzExQ0Y5RkYxQ0U0RUE1MDYyMTFDRjc4REM0QUU+XQovSW5mbyAxIDAgUgovUm9vdCAyIDAgUgovU2l6ZSA4Cj4+CnN0YXJ0eHJlZgo4MjgKJSVFT0YK\"]";

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

关于c# - 使用 Newtonsoft 反序列化 Json 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43810211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com