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

1
  • 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. Commented May 20, 2012 at 17:44

2 Answers 2

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?

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

2 Comments

How can a custom serializer convert using data that does not exist? (The wrapper class fields are not in the json)
you must override the ReadJson method from CustomCreationConverter.
1
var list = /*deserialize json list here*/;
var result = new PersonList() { PersonList = list };

2 Comments

This is not what I am looking for.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.