Elad Benda
Elad Benda

Reputation: 36682

JSON deserialization to object fails

return jsSerializer.Deserialize<MamDataResponseHolder>(stringJson);

throws an exception:

Type 'System.String' is not supported for deserialization of an array.

But I don't see the problem.

 public class MamDataResponseHolder
    {
        public MamDataResponsePair[] configuration { get; set; }
        public string Status { get; set; }
    }

  public class MamDataResponsePair
    {
        public string id { get; set; }
        public MamDataResponsecriteria[] criterias { get; set; }
    }

 public class MamDataResponsecriteria
    {
        public Guid criteriaId { get; set; }
        public string[] domains { get; set; }
        public string domainsException { get; set; }
    }

And here is the json:

    {
        "configuration": [{
            "id": "Coupon Body",
            "criterias": [{
                "criteriaId": "c7150fc2-72b9-4628-a199-dd5c0bdeef1b",
                "domains": [""],
                "domainsException": [""]
            }]
        }],
        "Status": "succeeded"
    }

Upvotes: 2

Views: 3676

Answers (2)

CodeGuru
CodeGuru

Reputation: 2803

Your Classes should be like below:

public class Criteria
{
    public string criteriaId { get; set; }
    public List<string> domains { get; set; }
    public List<string> domainsException { get; set; }
}

public class Configuration
{
    public string id { get; set; }
    public List<Criteria> criterias { get; set; }
}

public class RootObject
{
    public List<Configuration> configuration { get; set; }
    public string Status { get; set; }
}

Upvotes: 1

Your model and your Json do not match. Take a look at domainsException. In your Json it's clearly a string-array, but in your model it's just a string.

Besides that: Are you sure you want [""] in your Json? That way you get an array with an empty string instead of an empty array.

Upvotes: 2

Related Questions