所以我把这个JArray
存储在object
类型的变量中
public object Errors { get; }
其中包含该
Errors = {[
{
"name": [
"Username "admin" has already been taken."
],
"email": [
"Email is not a valid email address."
]
}
]}
或者它将包含这个
Errors = {[
"User not found."
]}
我希望能够提取这样的数据。。email: "Email is not a valid email address."
我不知道该怎么做,这就是我目前所拥有的
foreach (var error in (JArray)Errors)
{
System.Console.WriteLine($"Type: {error.Type}");
}
如何正确提取错误,以便获得包含kvp或email: "Email is not a valid email address."
等字符串的集合
这样的东西怎么样?
static List<string> ExtractErrors(JArray array)
{
JToken item = array.FirstOrDefault();
if (item.Type == JTokenType.Null)
{
return new List<string>();
}
if (item.Type == JTokenType.Object)
{
return item.Children<JProperty>()
.Select(jp => jp.Name + ": " + (string)jp.Value.FirstOrDefault())
.ToList();
}
return new List<string> { item.ToString() };
}
下面是一个演示:https://dotnetfiddle.net/md1oyn