0
{
    "users": [
        {
            "name": "User1",
            "email": "[email protected]",
            "phone": "+12049987456",
            "status": "Active",
            "title": "Mr"
        },
        {
            "name": "User2",
            "email": "[email protected]",
            "phone": "+12040147456",
            "status": "Active",
            "title": "Mr"
        },
        {
            "name": "User3",
            "email": "[email protected]",
            "phone": "+12040787456",
            "status": "Active",
            "title": "Ms"
        }
    ]
}

Tried Converting this json object to a List but it is only having null values

var a = JsonConvert.DeserializeObject<User>(jsonData);

How can i Parse it to a Class Object ?

1

3 Answers 3

2

If you want to deserialize that particular json string you need to define a root object that contain the list of your users.

For example:

public class ListRoot
{ 
    public List<User> users { get; set; }
}

public class User
{ 
    public string name { get; set; }
    public string phone { get; set; }
    public string email { get; set; }
    public string status { get; set; }
    public string title { get; set; }
}

And now you can call

var data = JsonConvert.DeserializeObject<ListRoot>(jsonData);
foreach (User u in data.users)
{
    Console.WriteLine($"User:name={u.name}, phone={u.phone}, email={u.email}");
}

Of course, if you can control the production of the json data, you could have a lot simpler approach preparing a json data like this

[
    {
        "name": "User1",
        "email": "[email protected]",
        "phone": "+12049987456",
        "status": "Active",
        "title": "Mr"
    },
    {
        "name": "User2",
        "email": "[email protected]",
        "phone": "+12040147456",
        "status": "Active",
        "title": "Mr"
    },
    {
        "name": "User3",
        "email": "[email protected]",
        "phone": "+12040787456",
        "status": "Active",
        "title": "Ms"
    }
]

that gives you the ability to call directly

List<User> users = JsonConvert.DeserializeObject<List<User>>(jsonData);
Sign up to request clarification or add additional context in comments.

Comments

1

You should convert to List of object, because your json is enumeration of users objects. For example: var a = JsonConvert.DeserializeObject<List<User>>(jsonData);

2 Comments

Did you try your answer with the json data proposed by the OP?
Json can be tried at json2csharp.com . Other think is what is the User class, because we don`t have the code.
1

This depends on your User class. Please provide the code.

Your json looks more like the representation of List<User>. If so you have to deserialize via JsonConvert.DeserializeObject<List<User>>(json).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.