Reputation: 1343
I am attempting to put a JSON response from a web service into an object that I can pass to my view and display the results of. I seem to be having some issues deserializing the JSON. Here is the error.
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Project.Models.Order' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.
My controller code is as follows.
string url = @"URL of webservice here";
var returnValue = new WebClient().DownloadString(url);
var obj = JsonConvert.DeserializeObject<Order>(returnValue);
return View(obj);
Here is my view.
@model Project.Models.Order
@foreach (var order in Model.sWorkOrderNumber)
{
@Model.sWorkOrderNumber
}
Here is the JSON that is getting returned.
[{"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"}]
Edit: Adding Order Model Code
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;
}
}
}
Thanks in advance for any help. I have tried every which way to get this to format correctly and cannot get it to work. Any guidance would be amazing.
Upvotes: 0
Views: 110
Reputation: 35514
Your model should look something like this. Note that ShipmentTrackingNUmbers
is a 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; }
}
Now you can deserialize the returnValue
as a List of Shipments
List<Shipment> shipments = JsonConvert.DeserializeObject<List<Shipment>>(returnValue);
Upvotes: 3