0

I am trying to Deserialize a Json Object into Dynamic List by order, so I have:

Json:

   string json = "{'elements':[
                     {'EntityA':[
                               {'name ':'Jhon'}
                               ],
                      'EntityB':[
                               {'title' : 'car'}
                               ],
                      'EntityB':[
                               {'title':'aaa'}
                               ],
                      'EntityA':[
                               {'name' : 'Alice'}
                               ]]}";

.NET Classes Base Classes:

public interface EntitysInterface{}

public class EntityA: EntitysInterface
{
   public string name { get; set; }
}

public class EntityB: EntitysInterface
{
   public string title { get; set; }
}

public class Entitys
{
    public List<EntitysInterface> elements { get; set; } //EntityA, EntityB,...

    public Entitys() 
    {
    }
}

My DeserializeObject Dificult:

Entitys listFinaly = JsonConvert.DeserializeObject<Entitys>(json); 

Exception "Type is an interface or abstract class and cannot be instantiated. :( "

1
  • Maybe, I would try to help if JsonConvert wouldn't have thrown exception while parsing your json string Commented Jan 10, 2013 at 23:35

1 Answer 1

2

The JSON.NET deserializer is telling you that it can't figure out which EntitysInterface object that you would like selected. It requires that you give it a hint.

ContractResolver helps to give that hint.

This post answers a similar question.

EDIT:

There is a good example in the JSON.Net documentation.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.