如何检查 Json 对象是否已填充所有值
How to Check if Json object has all values filled
我从外部 api 获得了一个很大的嵌套 json 对象。我想确保所有字段都填充在 json 对象中。有没有图书馆可以做到这一点? Newtonsoft.Json
有 JToken
class 检查 Json 的架构是否有效但我没有找到任何方法来检查 [=28] 中的所有字段=] 对象被填充。
场景: 我正在构建一个 api 来收集有关个人或实体的信息。有许多信息来源。我需要继续搜索数据,直到所需的对象已满。所以首先调用 api1,获取一些数据,检查对象是否已满。如果对象未满则转到 api2 等等。所以对象满后调用returns。 一个关键点是所需的对象模式不是静态的。
我可以将它反序列化为 POCO 并遍历每个嵌套对象,但我正在寻找更好的解决方案。
非常感谢任何建议。
如果我没理解错的话,你有一些复杂的 JSON 对象,有一组必需和不需要的属性。
我在这里看到三个解决方案:
- 您已经提到 JSON 模式验证。这是实现之一:
Json.NET Schema
.
创建一个 POCO 对象并用相应的 Required/Not-Required 属性标记所有属性。在这种情况下,如果 json 字符串不包含任何强制数据,您将获得异常。例如,它是如何根据 Json.NET's JsonPropertyAttribute
:
完成的
[JsonObject]
public class PocoObject
{
[JsonProperty("$schema", Required = Required.Default, NullValueHandling = NullValueHandling.Ignore)]
public string Schema { get; set; }
[JsonProperty("name", Required = Required.Always)]
public string Name { get; set; }
[JsonProperty("properties", Required = Required.Always)]
public MorePropertiesObject Properties { get; set; }
}
作为奖励,您可以添加自定义属性、方法、转换器等
将原始 json 反序列化为类似字典的结构,并使用您自己的手写验证器对其进行验证。类似于:
try
{
var jo = JObject.Parse(jsonString);
Contract.Assert(!string.IsNullOrEmpty(jo["root"]["prop1"].ToString()));
Contract.Assert(!string.IsNullOrEmpty(jo["root"]["prop2"].ToString()));
}
catch (JsonReaderException) { }
catch (JsonSerializationException) { }
提供的代码示例是您提到的 Newtonsoft.Json
如果您有一个 JSON 字符串并且只想检查任何 属性 值或数组项是否为 null
,您可以解析为 , recursively descend the JToken
hierarchy with JContainer.DescendantsAndSelf()
,并且使用以下扩展方法检查每个 JValue
是否为 null
:
public static partial class JsonExtensions
{
public static bool AnyNull(this JToken rootNode)
{
if (rootNode == null)
return true;
// You might consider using some of the other checks from JsonExtensions.IsNullOrEmpty()
// from
return rootNode.DescendantsAndSelf()
.OfType<JValue>()
.Any(n => n.Type == JTokenType.Null);
}
public static IEnumerable<JToken> DescendantsAndSelf(this JToken rootNode)
{
if (rootNode == null)
return Enumerable.Empty<JToken>();
var container = rootNode as JContainer;
if (container != null)
return container.DescendantsAndSelf();
else
return new[] { rootNode };
}
}
然后做:
var root = JToken.Parse(jsonString);
var anyNull = root.AnyNull();
如果您只想检查空 属性 值(即数组中的空值是可以的)您可以使用以下扩展方法:
public static partial class JsonExtensions
{
public static bool AnyNullPropertyValues(this JToken rootNode)
{
if (rootNode == null)
return true;
return rootNode.DescendantsAndSelf()
.OfType<JProperty>()
.Any(p => p.Value == null || p.Value.Type == JTokenType.Null);
}
}
(从你的问题中还不完全清楚你想要哪个。)
样本.Net fiddle。
我从外部 api 获得了一个很大的嵌套 json 对象。我想确保所有字段都填充在 json 对象中。有没有图书馆可以做到这一点? Newtonsoft.Json
有 JToken
class 检查 Json 的架构是否有效但我没有找到任何方法来检查 [=28] 中的所有字段=] 对象被填充。
场景: 我正在构建一个 api 来收集有关个人或实体的信息。有许多信息来源。我需要继续搜索数据,直到所需的对象已满。所以首先调用 api1,获取一些数据,检查对象是否已满。如果对象未满则转到 api2 等等。所以对象满后调用returns。 一个关键点是所需的对象模式不是静态的。
我可以将它反序列化为 POCO 并遍历每个嵌套对象,但我正在寻找更好的解决方案。
非常感谢任何建议。
如果我没理解错的话,你有一些复杂的 JSON 对象,有一组必需和不需要的属性。
我在这里看到三个解决方案:
- 您已经提到 JSON 模式验证。这是实现之一:
Json.NET Schema
. 创建一个 POCO 对象并用相应的 Required/Not-Required 属性标记所有属性。在这种情况下,如果 json 字符串不包含任何强制数据,您将获得异常。例如,它是如何根据 Json.NET's
完成的JsonPropertyAttribute
:[JsonObject] public class PocoObject { [JsonProperty("$schema", Required = Required.Default, NullValueHandling = NullValueHandling.Ignore)] public string Schema { get; set; } [JsonProperty("name", Required = Required.Always)] public string Name { get; set; } [JsonProperty("properties", Required = Required.Always)] public MorePropertiesObject Properties { get; set; } }
作为奖励,您可以添加自定义属性、方法、转换器等
将原始 json 反序列化为类似字典的结构,并使用您自己的手写验证器对其进行验证。类似于:
try { var jo = JObject.Parse(jsonString); Contract.Assert(!string.IsNullOrEmpty(jo["root"]["prop1"].ToString())); Contract.Assert(!string.IsNullOrEmpty(jo["root"]["prop2"].ToString())); } catch (JsonReaderException) { } catch (JsonSerializationException) { }
提供的代码示例是您提到的 Newtonsoft.Json
如果您有一个 JSON 字符串并且只想检查任何 属性 值或数组项是否为 null
,您可以解析为 JToken
hierarchy with JContainer.DescendantsAndSelf()
,并且使用以下扩展方法检查每个 JValue
是否为 null
:
public static partial class JsonExtensions
{
public static bool AnyNull(this JToken rootNode)
{
if (rootNode == null)
return true;
// You might consider using some of the other checks from JsonExtensions.IsNullOrEmpty()
// from
return rootNode.DescendantsAndSelf()
.OfType<JValue>()
.Any(n => n.Type == JTokenType.Null);
}
public static IEnumerable<JToken> DescendantsAndSelf(this JToken rootNode)
{
if (rootNode == null)
return Enumerable.Empty<JToken>();
var container = rootNode as JContainer;
if (container != null)
return container.DescendantsAndSelf();
else
return new[] { rootNode };
}
}
然后做:
var root = JToken.Parse(jsonString);
var anyNull = root.AnyNull();
如果您只想检查空 属性 值(即数组中的空值是可以的)您可以使用以下扩展方法:
public static partial class JsonExtensions
{
public static bool AnyNullPropertyValues(this JToken rootNode)
{
if (rootNode == null)
return true;
return rootNode.DescendantsAndSelf()
.OfType<JProperty>()
.Any(p => p.Value == null || p.Value.Type == JTokenType.Null);
}
}
(从你的问题中还不完全清楚你想要哪个。)
样本.Net fiddle。