我正在尝试将来自Web服务的JSON响应放入一个对象,我可以将其传递给我的视图并显示结果。我似乎有一些问题反序列化JSON。这是错误。
无法将当前JSON数组(例如[1,2,3])反序列化为类型' Project.Models.Order'因为类型需要JSON对象(例如{" name":" value"})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{" name":" value"})或将反序列化类型更改为实现集合接口的数组或类型(例如ICollection,IList)就像可以从JSON数组反序列化的List。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。 路径'',第1行,第1位。
我的控制器代码如下。
string url = @"URL of webservice here";
var returnValue = new WebClient().DownloadString(url);
var obj = JsonConvert.DeserializeObject<Order>(returnValue);
return View(obj);
以下是我的观点。
@model Project.Models.Order
@foreach (var order in Model.sWorkOrderNumber)
{
@Model.sWorkOrderNumber
}
这是返回的JSON。
[{"ShipmentTrackingNUmbers":[{"sTrackingNumber":"Redacted"}],"dtDateShipped":"5/12/2017 12:00:00 AM","dSPRatedWeight":"41.0000","nMBTotalBoxes":"1","sShipToAddressLine2":"Redacted","sShipToCountry":"U.S.A.","sShipToZip":"Redacted","sShipToState":"Redacted","sShipToCity":"Redacted","sShipToAddressLine1":"Redacted","sShipToName":"Redacted","sServiceLevel":"GND","sInvoiceNumber":"","dtInvoiceDate":"","mInvoiceAmount":"","dtDateReceived":"5/8/2017 3:16:25 PM","sStudioCategory2":"","sStudioCategory1":"","sOrderSource":"Redacted","sCustomerComments":"Redacted","sCustomerOrderName":"Redacted","sWorkOrderNumber":"Redacted","sCustomerNumber":"Redacted"},{"ShipmentTrackingNUmbers":[{"sTrackingNumber":"Redacted"}],"dtDateShipped":"5/10/2016 12:00:00 AM","dSPRatedWeight":"3.0000","nMBTotalBoxes":"1","sShipToAddressLine2":"","sShipToCountry":"U.S.A.","sShipToZip":"Redacted","sShipToState":"Redacted","sShipToCity":"Redacted","sShipToAddressLine1":"Redacted","sShipToName":"Redacted","sServiceLevel":"GND","sInvoiceNumber":"","dtInvoiceDate":"","mInvoiceAmount":"","dtDateReceived":"5/2/2016 7:18:41 AM","sStudioCategory2":"","sStudioCategory1":"","sOrderSource":"Redacted","sCustomerComments":"Redacted","sCustomerOrderName":"Redacted","sWorkOrderNumber":"Redacted","sCustomerNumber":"Redacted"}]
编辑:添加订单型号代码
namespace Project.Models
{
public class Order
{
public string ShipmentTrackingNUmbers
{
get;
set;
}
public string dtDateShipped
{
get;
set;
}
public string dSPRatedWeight
{
get;
set;
}
public string nMBTotalBoxes
{
get;
set;
}
public string sShipToAddressLine2
{
get;
set;
}
public string sShipToCountry
{
get;
set;
}
public string ShipToZip
{
get;
set;
}
public string sShipToState
{
get;
set;
}
public string sShipToCity
{
get;
set;
}
public string sShipToAddressLine1
{
get;
set;
}
public string sShipToName
{
get;
set;
}
public string sServiceLevel
{
get;
set;
}
public string sInvoiceNumber
{
get;
set;
}
public string dtInvoiceDate
{
get;
set;
}
public string mInvoiceAmount
{
get;
set;
}
public string dtDateReceived
{
get;
set;
}
public string sStudioCategory2
{
get;
set;
}
public string sStudioCategory1
{
get;
set;
}
public string sOrderSource
{
get;
set;
}
public string sCustomerComments
{
get;
set;
}
public string sCustomerOrderName
{
get;
set;
}
public string sWorkOrderNumber
{
get;
set;
}
public string sCustomerNumber
{
get;
set;
}
}
}
提前感谢您的帮助。 我已经尝试了各种方法来正确格式化,无法使其工作。任何指导都会很棒。
答案 0 :(得分:3)
你的模型看起来应该是这样的。请注意,ShipmentTrackingNUmbers
是List
class Shipment
{
public List<ShipmentTrackingNUmbers> ShipmentTrackingNUmbers { get; set; }
public string dtDateShipped { get; set; }
public string dSPRatedWeight { get; set; }
public string nMBTotalBoxes { get; set; }
public string sShipToAddressLine2 { get; set; }
}
public class ShipmentTrackingNUmbers
{
public string sTrackingNumber { get; set; }
}
现在,您可以将returnValue
反序列化为货件清单
List<Shipment> shipments = JsonConvert.DeserializeObject<List<Shipment>>(returnValue);