简单介绍
nlohmann::basic_json::contains
是一个成员函数,用于检查 nlohmann::json
对象是否包含特定的键。nlohmann::json
是一个流行的 C++ JSON 库,由 Niels Lohmann 开发,用于处理 JSON 数据。
函数原型如下:
bool contains( const KeyT& key ) const noexcept;
这个函数接受一个键(通常是一个字符串)作为参数,并返回一个布尔值。如果 nlohmann::json
对象包含给定的键,那么函数返回 true
,否则返回 false
。
这是一个使用示例:
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// 创建一个 JSON 对象
json j = {{"key1", "value1"}, {"key2", "value2"}};
// 检查是否包含 "key1"
if (j.contains("key1")) {
std::cout << "Contains 'key1'\n";
} else {
std::cout << "Does not contain 'key1'\n";
}
// 检查是否包含 "key3"
if (j.contains("key3")) {
std::cout << "Contains 'key3'\n";
} else {
std::cout << "Does not contain 'key3'\n";
}
return 0;
}
在这个例子中,第一个 if
语句会输出 “Contains ‘key1’”,因为 j
包含 “key1”。第二个 if
语句会输出 “Does not contain ‘key3’”,因为 j
不包含 “key3”。
注意,contains
函数只能用于对象类型的 nlohmann::json
。如果你尝试在一个非对象类型(如数组或字符串)上调用它,编译器会报错。
以下是调整格式后的翻译:
nlohmann::basic_json::contains
// (1)
bool contains(const typename object_t::key_type& key) const;
// (2)
template<typename KeyType>
bool contains(KeyType&& key) const;
// (3)
bool contains(const json_pointer& ptr) const;
- 检查是否在具有等效键
key
的 JSON 对象中存在元素。如果未找到元素或 JSON 值不是对象,则返回false
。 - 参见 1。此重载仅在
KeyType
可与typename object_t::key_type
比较且typename object_comparator_t::is_transparent
表示类型时可用。 - 检查给定的 JSON 指针
ptr
是否可以在当前的 JSON 值中解析。
模板参数
KeyType
:除json_pointer
之外的对象键的类型,该类型可以使用object_comparator_t
与string_t
进行比较。这也可以是字符串视图(C++17)。
参数
key
(输入):要检查其存在的键值。ptr
(输入):要检查其存在的 JSON 指针。
返回值
- 如果存在指定
key
的元素,则返回true
。如果未找到此类键的元素或 JSON 值不是对象,则返回false
。 - 参见 1。
- 如果 JSON 指针可以解析为存储的值,则返回
true
,否则返回false
。
异常安全性
强异常安全性:如果发生异常,原始值保持不变。
异常
- 该函数不会抛出异常。
- 该函数不会抛出异常。
- 该函数可能会抛出以下异常:
- 如果数组索引以
0
开始,则抛出parse_error.106
。 - 如果数组索引不是数字,则抛出
parse_error.109
。
- 如果数组索引以
复杂性
在 JSON 对象的大小中对数。
注意事项
- 当在不是对象的 JSON 类型上执行此方法时,此方法始终返回
false
。 - 此方法可以在任何 JSON 值类型上执行。
示例
示例1:
此示例显示了如何使用 contains()
。
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{
// 创建一些 JSON 值
json j_object = R"( {"key": "value"} )"_json;
json j_array = R"( [1, 2, 3] )"_json;
// 调用 contains
std::cout << std::boolalpha
以下是调整格式后的翻译:
# nlohmann::basic_json::contains
```cpp
// (1)
bool contains(const typename object_t::key_type& key) const;
// (2)
template<typename KeyType>
bool contains(KeyType&& key) const;
// (3)
bool contains(const json_pointer& ptr) const;
- 检查是否在具有等效键
key
的 JSON 对象中存在元素。如果未找到元素或 JSON 值不是对象,则返回false
。 - 参见 1。此重载仅在
KeyType
可与typename object_t::key_type
比较且typename object_comparator_t::is_transparent
表示类型时可用。 - 检查给定的 JSON 指针
ptr
是否可以在当前的 JSON 值中解析。
示例2
#include <iostream>
#include <string_view>
#include <nlohmann/json.hpp>
using namespace std::string_view_literals;
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{
// create some JSON values
json j_object = R"( {"key": "value"} )"_json;
json j_array = R"( [1, 2, 3] )"_json;
// call contains
std::cout << std::boolalpha <<
"j_object contains 'key': " << j_object.contains("key"sv) << '\n' <<
"j_object contains 'another': " << j_object.contains("another"sv) << '\n' <<
"j_array contains 'key': " << j_array.contains("key"sv) << std::endl;
}
示例3
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann::literals;
int main()
{
// create a JSON value
json j =
{
{"number", 1}, {"string", "foo"}, {"array", {1, 2}}
};
std::cout << std::boolalpha
<< j.contains("/number"_json_pointer) << '\n'
<< j.contains("/string"_json_pointer) << '\n'
<< j.contains("/array"_json_pointer) << '\n'
<< j.contains("/array/1"_json_pointer) << '\n'
<< j.contains("/array/-"_json_pointer) << '\n'
<< j.contains("/array/4"_json_pointer) << '\n'
<< j.contains("/baz"_json_pointer) << std::endl;
try
{
// try to use an array index with leading '0'
j.contains("/array/01"_json_pointer);
}
catch (json::parse_error& e)
{
std::cout << e.what() << '\n';
}
try
{
// try to use an array index that is not a number
j.contains("/array/one"_json_pointer);
}
catch (json::parse_error& e)
{
std::cout << e.what() << '\n';
}
}
结语
在我们的编程学习之旅中,理解是我们迈向更高层次的重要一步。然而,掌握新技能、新理念,始终需要时间和坚持。从心理学的角度看,学习往往伴随着不断的试错和调整,这就像是我们的大脑在逐渐优化其解决问题的“算法”。
这就是为什么当我们遇到错误,我们应该将其视为学习和进步的机会,而不仅仅是困扰。通过理解和解决这些问题,我们不仅可以修复当前的代码,更可以提升我们的编程能力,防止在未来的项目中犯相同的错误。
我鼓励大家积极参与进来,不断提升自己的编程技术。无论你是初学者还是有经验的开发者,我希望我的博客能对你的学习之路有所帮助。如果你觉得这篇文章有用,不妨点击收藏,或者留下你的评论分享你的见解和经验,也欢迎你对我博客的内容提出建议和问题。每一次的点赞、评论、分享和关注都是对我的最大支持,也是对我持续分享和创作的动力。
阅读我的CSDN主页,解锁更多精彩内容:泡沫的CSDN主页