0

I would like to convert following Json Array to .net list wrapper. Not sure how ca I do that.

Json Array:

[{"Name" : "SomeName1", "Age" : "20" },{ "Name" : "SomeName2", "Age" : "21"}]

My class is:

public class Person
{
    public string Name;
    public string Age;
}

If I use List<Person> as my conversion type then everything works fine.

But what I would like to do is to convert above array to following class's object;

public class PersonList
{
    public string somefield;
    public List<Person> PersonList;
}

I am not able to convert my array to List wrapper object. How can I do that?

I do not have control over conversion method. I am using RestSharp library to execute my web request. When I call execute method, I would like to pass PersionList type for conversion and not List

Tejas Vora
  • 538
  • 9
  • 19
  • 1
    `what I would like to do is to convert above array to following class's object` But your json string represents an array of objects, not an object containing array. – L.B May 20 '12 at 17:44

2 Answers2

1
var list = /*deserialize json list here*/;
var result = new PersonList() { PersonList = list };
usr
  • 168,620
  • 35
  • 240
  • 369
  • This is not what I am looking for. – Tejas Vora May 20 '12 at 20:12
  • 3
    This really seems to be what you are looking for, but you just don't know it. Your jsom does not have the properties of the wrapper class - it has the properties of. List of person. It would be easy to construct your wrapper if you had "somefield" available. – Andrew Barber May 21 '12 at 00:12
1

You want to implement a custom JSON Converter. Here is an example : How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

Community
  • 1
  • 1
Byronic Coder
  • 84
  • 1
  • 1
  • 8