简体   繁体   中英

How to deserialize JSON nested object array to c# object

I have been stumped with this and unable to proceed. I am also not very experienced yet so your help would be appreciated. I am using an API that returns the following data:

{"userdata":[{"IdCustomer":"4","IdShop_group":"1","IdShop":"2","IdGender":"1","IdDefault_group":"3","IdLang":"2","Company":null,"Firstname":"John","Lastname":"Doe","Email":"johndoe@email.com","NumpadPIN":"99999999","Birthday":"1991-12-24","Newsletter":"0","Optin":"0","Active":"1","Date_add":"2019-04-09 07:37:29","Date_upd":"2019-07-17 11:38:25"}]}

I have the following classes set up by using 'json2csharp'

public class Userdata
{
    public string IdCustomer { get; set; }
    public string IdShop_group { get; set; }
    public string IdShop { get; set; }
    public string IdGender { get; set; }
    public string IdDefault_group { get; set; }
    public string IdLang { get; set; }
    public object Company { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
    public string NumpadPIN { get; set; }
    public string Birthday { get; set; }
    public string Newsletter { get; set; }
    public string Optin { get; set; }
    public string Active { get; set; }
    public string Date_add { get; set; }
    public string Date_upd { get; set; }
}

    public class User
{
    public IList<Userdata> userdata { get; set; }
}

And here's how I pull the data

//changed the url for this question
    var URL = "https://dummyvalue.com";

                    var request = new HttpRequestMessage();
                    request.RequestUri = new Uri(URL);
                    request.Method = HttpMethod.Get;
                    request.Headers.Add("Accept", "application/json");
                    var client = new HttpClient();
                    HttpResponseMessage response = await client.SendAsync(request);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {

                        ApiStatusLabel.Text = "200";

                        HttpContent content = response.Content;
                        var json = await content.ReadAsStringAsync();

                        // create new object
                        User userObject = JsonConvert.DeserializeObject<User>(json);

                    }

My question is on how do I assign/use the values I have deserialized? I would like to access the values as class object details ex.

var name = userObject.Firstname;
var surname = userObject.Lastname;

I have read a lot and tried different ways but I must be missing something..

EDIT - fixed the class name from RootObject to User

You need to use RootObject

var response = JsonConvert.DeserializeObject<RootObject>(json);
foreach(var userObject in response.userdata)
{
    //use here userObject.Firstname;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM