通过json_pointer可以不必一层一层的读取或设置json值,可以直接完成。
其实json类似一个树,可以通过从根节点/开始,指定每一个节点及叶子节点的名字,然后直接访问json数据:
#include <iostream>
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
int main()
{
json data = R"({
"name": "xiaoming",
"age": 10,
"parent":
[
{
"father" : "zhang",
"age" : 40
},
{
"mother" : "wang",
"age" : 36
}
]
})"_json;
cout<<data["/name"_json_pointer]<<endl;
cout<<data["/