I have a JObject
variable UserPreferences
(C#); It will contain dynamic key value pairs based on request.
It is guaranteed that the key exists. I just want to check if the key has an empty value so that I can throw a "Missing fields" exception following which user can then fill the fields and send request again. How can I achieve this (preferably with iteration over the keys present in UserPreferences object [below])? A controller receives the request and maps it to a User class object. How do I check if there is any empty value in any key?
My requests will be of the form:
{
"UserName":"Mark",
"UserPreferences":{
"Section1":"Do",
"Section2":"Delete"
}
}
or it can also be like:
{
"UserName":"Mark",
"UserPreferences":{
"Section1":"Do",
"Section2":"Delete",
"Section4":"Add_Name"
}
}
Hence, the request can contain dynamic number of key value pairs in UserPreferences object (more sections and their values); But sometimes a user may leave the field empty. So, when the request arrives, it will look something like this:
{
"UserName":"Mark",
"UserPreferences":{
"Section1":"",
"Section2":""
}
}
or it can look like this (another example):
{
"UserName":"Mark",
"UserPreferences":{
"Section1":"Do",
"Section2":""
}
}
Here's what my class that handles the request looks like:
public class User
{
public string User{get;set;}
public JObject UserPreferences{get;set;}
}
Note that I do not want to use any other structure like dictionary as a complete replacement of the JObject structure used above.
Parse UserPreferences
as Dictionary<string, string>
and check dictionary value to be not null:
public class User
{
public string UserName { get; set; }
public Dictionary<string, string> UserPreferences { get; set; }
}
User u = ... // parse json
if(u.UserPreferences.Values.Any(v => v == null))
{
...
}
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.