I have declared a helper class to create a JSON. But I just doesn not want to work properly with Lists. Any suggestions to this code? The error occours when trying to set roleDescription and roleType value.
Error: ‘List< generateCredentialsRoleList>’ does not contain a definition for ‘roleDescription ‘
Error: ‘List< generateCredentialsRoleList>’ does not contain a definition for ‘roleType’
public class generateTokenRequest
{
public generateToken generateToken { get; set; }
}
public class generateToken
{
public List<generateCredentialsRoleList> credentialsRoleList { get; set; }
public string description { get; set; }
}
public class generateCredentialsRoleList
{
public string roleDescription { get; set; }
public string roleType { get; set; }
}
public string generateTokenRequest(string roleDescription, string roleType, string description)
{
generateTokenRequest generateTokenRequest = new generateTokenRequest
{
generateToken = new generateToken
{
credentialsRoleList = new List<generateCredentialsRoleList>
{
roleDescription = roleDescription,
roleType = roleType,
},
description = description,
}
};
string json = JsonConvert.SerializeObject(generateTokenRequest, Newtonsoft.Json.Formatting.Indented);
return json;
}
2
Answers
You need to add new item in list initializer like this:
Here:
you try to initialize a list. A list takes items. You don’t provide items, you provide property values of an item.
How to fix this depends on what you actually want to do. Do you want to initialize the list with a single item containing those properties? That would be done as follows:
Some additional recommendations:
generateCredentialsRoleList
is not a good name of the item in a list. It’s like calling a single apple a "bag of apples".