如何检查 json 对象中的任何键是否具有空值
JsonObject itemObject = itemValue.GetObject();
string id = itemObject["id"].GetString() == null ? "" : itemObject["id"].GetString();
this is my code but app crashes on it if null value for key "id"
请您参考如下方法:
IJsonValue idValue = itemObject.GetNamedValue("id");
if ( idValue.ValueType == JsonValueType.Null)
{
// is Null
}
else if (idValue.ValueType == JsonValueType.String)
{
string id = idValue.GetString();
}
如果你这样做太多,考虑添加 extension methods .
相反的用法:
IJsonValue value = JsonValue.CreateNullValue();
阅读here有关空值的更多信息。
