nlohmann json:通过items遍历object/array

//官方的例子
#include 
#include 

using json = nlohmann::json;

int main()
{
    // create JSON values
    json j_object = {
  {"one", 1}, {"two", 2}};
    json j_array = {1, 2, 4, 8, 16};

    // example for an object
    for (auto& x : j_object.items())
    {
        std::cout << "key: " << x.key() << ", value: " << x.value() << '\n';
    }

    // example for an array
    for (auto& x : j_array.items())
    {
        std::cout << "key: " << x.key() << ", value: " << x.value() << '\n';
    }
}

编译运行输出:

key: one, value: 1
key: two, value: 2
key: 0, value: 1
key: 1, value: 2
key: 2, value: 4
key: 3, value: 8
key: 4, value: 16 

可以看到对于object可以通过key()和value()拿到键值对

你可能感兴趣的:(C/C++,json)