1

Consuming an external service where they decided to provide an object as an array where each property is on a fixed position. Like

{
    "persons" : [
        ["Luck", "Lucky", 28],
        ["Joe", "Dalton", 30],
        ["Jack", "Dalton", 28],
        ["William", "Dalton", 26],
        ["Averell", "Dalton", 24]
    ]
}

I would like to deserialize this to a List of Person.

var persons =  JsonConvert.DeserializeObject<Person[]>(json);

class Person {
      public string FirstName {get; set; }
      public string LastName {get; set; }
      public int Age{get; set; }
}

Is there an easy way (property annotation) to do this, or will it come down to write a custom serialiser?

Since most answers are missing the essence of the question.

This would have been easy if

  {
    "persons" : [
        { 
             "FirstName" :  "Luck", 
             "LastName" : "Lucky", 
             "Age" : 28 
        },
        // ...
    ]
}

But that is not the case.

Stijn Van Antwerpen
  • 1,840
  • 17
  • 42
  • 1
    I presume you mis-copied your example as it's not valid JSON (persons needs to be "persons"). – Neil Jul 14 '20 at 07:42

7 Answers7

2

You can do it very easy and simple with this:

PersonList personObject = new PersonList();


public class PersonList
{
    public List<Person> persons { get; set; }
}

public class Person
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string Age{ get; set; }
}

personObject  = JsonConvert.DeserializeObject<PersonList>(YourResponseContent);

And then the personObject items will have the values of the Response.

The MainList have to have the same name with your item in the Response. This is important to read the JSON Response.

LopDev
  • 823
  • 10
  • 26
2

persons property is array of arrays of strings. Try to deserialize to List<List<string>> first

public class Root
{
    public List<List<string>> persons { get; set; }
}

and then convert it to list of List<Person> using Select method

var root = JsonConvert.DeserializeObject<Root>(jsonString);
var persons = root.persons
    .Select(l => new Person { FirstName = l[0], LastName = l[1], Age = int.Parse(l[2]) })
    .ToList();
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
1

You should create a custom json deserializer.

Try this:

public class PersonJsonConverter : JsonConverter
{
    public class RootJson
    {
        public List<List<string>> Persons { get; set; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jsonObject = JObject.Load(reader);
        var root = JsonConvert.DeserializeObject<RootJson>(jsonObject.Root.ToString());

        var persons = root.Persons
            .Select(p => new Person { FirstName = p[0], LastName = p[1], Age = int.Parse(p[2]) })
            .ToList();

        return persons;
    }

    public override bool CanConvert(Type objectType)
    {
        return true;
    }
}

And you can derserialize like this:

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }
}

 var deserializedRoot = JsonConvert.DeserializeObject<List<Person>>(yourJson, new PersonJsonConverter());
0

You can Deserialize list of objects in C#

var results=JsonConvert.DeserializeObject<List<Person>>(json);
rlm96
  • 193
  • 1
  • 15
0

How about

public class Root
{
    public List<List<Object>> persons { get; set; }

}

and

Root dsz = JsonConvert.DeserializeObject<Root>(sz);

It will be deserialized to list of persons each person will have list of vars.

persons[0][0] = "Luck"
InUser
  • 1,138
  • 15
  • 22
0

If I understood your question correctly, you can do something like that:

public class PersonObj
{
    public List<Person> persons {get;set;}
    public string FirstName { get { return persons.First(); }
    public string LastName { get { return return persons.Skip(1).First(); } }
}
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
0
class ParsedList
    {
        public List<List<object>> persons { get; set; }
    }
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

text is your JSON:

ParsedList list = Newtonsoft.Json.JsonConvert.DeserializeObject<ParsedList>(text);

            List<Person> people = list.persons.Select(x => new Person()

            {
                FirstName = x[0].ToString(),
                LastName = x[1].ToString(),
                Age = Convert.ToInt32(x[2])
            }
            
            ).ToList();