0

Question background:

I'm receving a JSON payload from a WebApi end point, from here I am mapping the data to a C# model structure through the use of Newtonsoft. The C# classes were produced using json2csharp.com

The Issue:

The following JSON (as viewed in the JSON viewer in Visual Studio) is causing an error on the Feature property of the ItemAttributes element when attempting to be mapped to its equivalent property on the C# class:

The Feature property reads:

"Feature: Polish Release, cover may contain Polish text/markings. The disk DOES NOT have English audio and subtitles."

enter image description here

The following shows the generated C# ItemAttributes class from json2csharp.com with the required 'Feature' property:

public class ItemAttributes
{
    public string Brand { get; set; }
    public string Department { get; set; }

    /* Feature string list*/
    public List<string> Feature { get; set; }

    public string Label { get; set; }
    public string Manufacturer { get; set; }
    public string NumberOfItems { get; set; }
    public string PackageQuantity { get; set; }
    public string ProductGroup { get; set; }
    public string ProductTypeName { get; set; }
    public string Publisher { get; set; }
    public string Studio { get; set; }
    public string Title { get; set; }
    public string Binding { get; set; }
    public string Color { get; set; }
    public PackageDimensions PackageDimensions { get; set; }
    public string Size { get; set; }
    public string IsAdultProduct { get; set; }
    public string MPN { get; set; }
    public string PartNumber { get; set; }
    public string SKU { get; set; }
}

The error that is being received:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred  in Newtonsoft.Json.dll but was not handled in user code

Additional information: Error converting value "Polish Release, cover may   contain Polish text/markings. The disk DOES NOT have English audio and subtitles." to type 'System.Collections.Generic.List`1[System.String]'. Path 'Items.Item[2].ItemAttributes.Feature', line 1, position 11935.

If anyone can see why I am having issues mapping the JSON Feature property to the C# equivalent that would be great.

---------------------------EDIT---------------------------

Thanks to Ivaylos answer I can now see that the issue is a the value of the string Feature property was trying to map to a string list in the C# equivilant.

The issue I now face is sometimes the JSON returns the Feature property as a list of strings and sometimes it is a single string.

i.e ideally I need two Feature propeties, a single string:

public string Property

and a list:

public List<string> Property

Obviously I cannot set two Feature properties in the ItemAttribute class.

How can I overcome this? I have no control over how the JSON is constructed

user1352057
  • 3,162
  • 9
  • 51
  • 117
  • You are having a feature property of type string, and you attempt to deserialize it as a list of strings. That is why you receive the exception. Either make the `Feature` property of type `string` or modify the JSON so that the feature member looks like this: `"Feature" : [ "Polish Release, ...." ]` (note the brackets `[ ]`) – Ivaylo Slavov Oct 18 '15 at 20:20
  • Can you show us the JSON itself, not via the visualizer? Specifically, the JSON you used to generate the classes. Also, if you're using VS2013 with the latest updates, have you tried generating C# classes via the Edit -> Paste Special -> Generate C# Classes from JSON? – Avner Shahar-Kashtan Oct 18 '15 at 20:22
  • @IvayloSlavov Thanks for your reply, I see exactly what you mean. The issue I face now is the payload from the API sometimes returns an array of strings for the 'feature' propetyand others time it sets it as a single string. Whats is the best way to handle this? To add, I have no control over the JSON structure. – user1352057 Oct 18 '15 at 20:26
  • 2
    Looks like a duplicate of [How to handle both a single item and an array for the same property using JSON.net](http://stackoverflow.com/questions/18994685/) or [How to handle json that returns both a string and a string array?](http://stackoverflow.com/questions/22052430/) then – dbc Oct 18 '15 at 20:43
  • Post your Json string over here. – Krunal Mevada Oct 19 '15 at 10:09

0 Answers0