Lempkin
Lempkin

Reputation: 1588

Deserialize json to list of object

I got the following string (JSON) from WS:

{"cables":"[{\"rexelReference\":\"FIL01084259\",\"providerReference\":\"1084259\",\"productLibe\":\"CABLE BLEU D\\u0027INSTRUM 01IT09EGFA\",\"brand\":\"FIL\",\"color\":\"BLEU\",\"section\":\"0,9\",\"conductorNumber\":1,\"displayProduct\":true},{\"rexelReference\":\"FIL01084386\",\"providerReference\":\"1084386\",\"productLibe\":\"CABLE BLEU D\\u0027INSTRUM 01IT09EGSF\",\"brand\":\"FIL\",\"color\":\"BLEU\",\"section\":\"0,9\",\"conductorNumber\":1,\"displayProduct\":true}]","productNumber":"2"}

and these objects :

public class ProductList
{
   [JsonProperty("cables")]
   public List<ProductDTO> cables { get; set; }
   public int count { get; set; }
}

public class ProductDTO
{
    public string rexelReference;
    public string providerReference;
    public string productLibe;
    public string brand;
    public string color;
    public string section;
    public string conductorNumber;
    public string displayProduct;
}

When I try to deserialize with this code :

ProductList list = JsonConvert.DeserializeObject<ProductList>(wsResponse2);

I get the error :

Additional information: Error converting value "[{" to type 'System.Collections.Generic.List`1[VoltaFront.DTO.ProductDTO]'. Path 'cables', line 1, position 14.

I don't understand the problem. Is it because of the backslashes? Am I suppose to remove them before deserialization?

Edit now the error i get is :

*Additional information: Error converting value "[{"rexelReference":"FIL01084259","providerReference":"1084259","productLibe":"CABLE BLEU D\u0027INSTRUM *<

Upvotes: 0

Views: 51

Answers (2)

Yousef
Yousef

Reputation: 81

You've a problem in the json format, just remove the not used double quotes and use it like the following:

string wsResponse2 = "{\"cables\":[{\"rexelReference\":\"FIL01084259\",\"providerReference\":\"1084259\",\"productLibe\":\"CABLE BLEU D\\u0027INSTRUM 01IT09EGFA\",\"brand\":\"FIL\",\"color\":\"BLEU\",\"section\":\"0,9\",\"conductorNumber\":1,\"displayProduct\":true}]}";

Upvotes: 2

mybirthname
mybirthname

Reputation: 18127

Your Json is not valid, after removing the escaping and put in JSON formatter and exception is thrown.

Removing Escape

JSON Formatter

Fix your JSON. Here is the error msg:

Unable to format the JSON input. Expected a ',' or '}' at character 15 near 'les":"[{"rexelRefere'

Upvotes: 0

Related Questions