Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;nlohmann json-如何迭代/查找嵌套对象_C++_Json_Nlohmann Json - Fatal编程技术网

C++ c++;nlohmann json-如何迭代/查找嵌套对象

C++ c++;nlohmann json-如何迭代/查找嵌套对象,c++,json,nlohmann-json,C++,Json,Nlohmann Json,我试图使用nlohmann::json迭代嵌套json。我的json对象如下: { "one": 1, "two": 2 "three": { "three.one": 3.1 }, } 我正在尝试迭代和/或查找嵌套对象。但是,似乎没有默认的支持。看起来我必须通过创建另一个循环来迭代每个子对象,或者为每个子对象递归调用fn 我下面的一段代码及其结果表明,只有顶级迭代是可能的 void findNPrintKey (json src, const

我试图使用nlohmann::json迭代嵌套json。我的json对象如下:

{
    "one": 1,
    "two": 2
    "three": {
        "three.one": 3.1
    },
}
我正在尝试迭代和/或查找嵌套对象。但是,似乎没有默认的支持。看起来我必须通过创建另一个循环来迭代每个子对象,或者为每个子对象递归调用fn

我下面的一段代码及其结果表明,只有顶级迭代是可能的

void findNPrintKey (json src, const std::string& key) {
  auto result = src.find(key);
  if (result != src.end()) {
    std::cout << "Entry found for : " << result.key() << std::endl;
  } else {
    std::cout << "Entry not found for : " << key << std::endl ;
  }
}


void enumerate () {

  json j = json::parse("{  \"one\" : 1 ,  \"two\" : 2, \"three\" : { \"three.one\" : 3.1 } } ");
  //std::cout << j.dump(4) << std::endl;

  // Enumerate all keys (including sub-keys -- not working)
  for (auto it=j.begin(); it!=j.end(); it++) {
    std::cout << "key: " << it.key() << " : " << it.value() << std::endl;
  }

  // find a top-level key
  findNPrintKey(j, "one");
  // find a nested key
  findNPrintKey(j, "three.one");
}

int main(int argc, char** argv) {
  enumerate();
  return 0;
}

那么,是否有一个递归迭代可用,或者我们必须自己使用is_object()方法来实现这一点?

确实,迭代不会递归,而且(目前)还没有用于此的库函数。那么:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

template<class UnaryFunction>
void recursive_iterate(const json& j, UnaryFunction f)
{
    for(auto it = j.begin(); it != j.end(); ++it)
    {
        if (it->is_structured())
        {
            recursive_iterate(*it, f);
        }
        else
        {
            f(it);
        }
    }
}

int main()
{
    json j = {{"one", 1}, {"two", 2}, {"three", {"three.one", 3.1}}};
    recursive_iterate(j, [](json::const_iterator it){
        std::cout << *it << std::endl;
    });
}

2017年以来有没有更好的办法?事实上,我对类似DOM的树遍历感兴趣,在这里我使用键来决定进一步的递归下降。但是,无法在某些类型的节点上对数组调用key(),也无法轻松检查迭代器。您可能需要尝试该函数。在遍历json树中的节点(项)时,是否有方法访问节点的父级?除非在遍历时存储它。
#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

template<class UnaryFunction>
void recursive_iterate(const json& j, UnaryFunction f)
{
    for(auto it = j.begin(); it != j.end(); ++it)
    {
        if (it->is_structured())
        {
            recursive_iterate(*it, f);
        }
        else
        {
            f(it);
        }
    }
}

int main()
{
    json j = {{"one", 1}, {"two", 2}, {"three", {"three.one", 3.1}}};
    recursive_iterate(j, [](json::const_iterator it){
        std::cout << *it << std::endl;
    });
}
1
"three.one"
3.1
2