Jordan Johnson
asked on
How can I deserialize this JSON (Unexpected character encountered while parsing value)?
I have no experience with deserialization, and am trying to figure out how to get this working. I am using JSON.NET, and attempts to deserialize come up with the following: Unexpected character encountered while parsing value: c. path '', line 0 position 0
I am currently working with the following JSON object:
Here is the PO Class:
Here is the deserialization code:
I am currently working with the following JSON object:
{
"id":"1024",
"OrderNumber":"1024",
"OrderDate":"11/17/2014 2:20:35 PM",
"ShippingMethod":"USPS Priority Mail",
"BillingAddress":{
"FirstName":"Joe",
"LastName":"Test1",
"Company":"",
"AddressLine1":"9 Hispalis Rd",
"AddressLine2":"",
"City":"Andover",
"State":"MA",
"Postcode":"01810",
"Country":"US",
"Email":"Joe@Test.com",
"Phone":"9785553902"
},
"ShippingAddress":{
"FirstName":"Joe",
"LastName":"Test",
"Company":"",
"AddressLine1":"9 Hispalis Rd",
"AddressLine2":"",
"City":"Andover",
"State":"MA",
"Postcode":"01810",
"Country":"US",
"Email":"Joe@Test.com",
"Phone":"9785553902"
},
"UpdateAt":"11/17/2014 2:21:08 PM",
"OrderStatus":"New",
"TotalAmount":22.70,
"PaidAmount":0.0,
"ShippingAmount":6.95,
"TaxAmount":0.00,
"OrderItems":[{
"ProductSKU":"PF7977",
"Quantity":"1.00",
"Price":"15.75",
"Discount":"0.00"
}],
"HandlingInstructions":null,
"SpecialInstructions":null,
"PromotionCode":null,
"SalesChannelId":"a57c68d2-c544-4f7a-8ed0-a39a00e2c1e0",
"Shipments":[{
"id":"1018",
"Carrier":
"USPS",
"ShippingService":"Priority",
"Status":"Completed",
"Box1":"1010",
"TrackingNumber1":"9405510200882424054304",
"ShippedDate1":"12/11/2014 12:56:40 AM",
"Weight1":"3.00",
"WeightUnit1":"lbs"}],
"DropShipPurchaseOrders":[{
"id":"1000",
"SupplierName":"Baldwin Filters",
"ShippingService":"USPS Parcel Post",
"ShipTo":"Paul Quirnbach",
"ShipmentId":"1019",
"SKU1":"PF7977",
"Quantity1":"1.00",
"SKU2":"BT7349",
"Quantity2":"3.00"}]
}
Here is the PO Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace FreestyleAPI
{
public class PO
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("OrderNumber")]
public int OrderNumber { get; set; }
[JsonProperty("OrderDate")]
public DateTime OrderDate { get; set; }
[JsonProperty("ShippingMethod")]
public string ShippingMethod { get; set; }
[JsonProperty("BillingAddress")]
public BillingAddress BillingAddress { get; set; } //requires class
[JsonProperty("ShippingAddress")]
public ShippingAddress ShippingAddress { get; set; } //requires class
[JsonProperty("UpdateAt")]
public DateTime UpdateAt { get; set; }
[JsonProperty("OrderStatus")]
public string OrderStatus { get; set; }
[JsonProperty("TotalAmount")]
public decimal TotalAmount { get; set; }
[JsonProperty("PaidAmount")]
public decimal PaidAmount { get; set; }
[JsonProperty("ShippingAmount")]
public decimal ShippingAmount { get; set; }
[JsonProperty("TaxAmount")]
public decimal TaxAmount { get; set; }
[JsonProperty("OrderItems")]
public OrderItems[] OrderItems { get; set; } //requires class
[JsonProperty("HandlingInstructions")]
public string HandlingInstructions { get; set; }
[JsonProperty("SpecialInstructions")]
public string SpecialInstructions { get; set; }
[JsonProperty("PromotionCode")]
public string PromotionCode { get; set; }
[JsonProperty("SalesChannelId")]
public string SalesChannelId { get; set; }
[JsonProperty("Shipments")]
public Shipments[] Shipments { get; set; } //requires class
[JsonProperty("DropShipPurchaseOrders")]
public DropshipPurchaseOrders[] DropShipPurchaseOrders { get; set; } //requires class
}
}
Here is the deserialization code:
logger.Log("loadJSON:" + strfile);
FileReference fileref = new FileReference();
fileref.filename = strfile;
PO POTest = new PO(); //instance of PO class
fileref.isValid = false;
//load the xml document
#region
try
{
POTest = JsonConvert.DeserializeObject<PO>(strfile);
Console.WriteLine("success,loaded JSON");
logger.Log("loaded JSON:" + strfile);
}
catch (Exception ex)
{
logger.Log("unable to load JSON doc:" + strfile + " " + ex.ToString());
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.