如何为现代C ++遍历JSON中的JSON

先生

我想遍历一个json对象中的每个条目,但是在另一个之后我遇到了一个无法理解的错误。以下示例如何更正?

#include <iostream>

#include <nlohmann/json.hpp>

using json = nlohmann::json;

void bla(std::string a) {
    std::cout << a << '\n'; 
}

int main() {
    json RecentFiles;

    RecentFiles["1"]["Name"] = "test1.txt";
    RecentFiles["1"]["Last modified"] = "monday";
    RecentFiles["1"]["Score"] = 5.0f;

    RecentFiles["2"]["Name"] = "test2.txt";
    RecentFiles["2"]["Last modified"] = "tuesday";
    RecentFiles["2"]["Score"] = 5.0f;


    for (auto it = RecentFiles.begin(); it != RecentFiles.end(); ++it) {
           bla("JSON: Recent file = " + it.value()["Name"]);    
    }

    std::cout << RecentFiles; }

错误:

prog.cc: In function 'int main()':
prog.cc:18:31: error: invalid conversion from 'const char*' to 'nlohmann::detail::iter_impl<nlohmann::basic_json<> >::difference_type {aka long int}' [-fpermissive]
         std::cout << it["Name"];
                               ^
In file included from prog.cc:2:0:
./nlohmann/json.hpp:4418:15: note: initializing argument 1 of 'nlohmann::detail::iter_impl<BasicJsonType>::reference nlohmann::detail::iter_impl<BasicJsonType>::operator[](nlohmann::detail::iter_impl<BasicJsonType>::difference_type) const [with BasicJsonType = nlohmann::basic_json<>; nlohmann::detail::iter_impl<BasicJsonType>::reference = nlohmann::basic_json<>&; nlohmann::detail::iter_impl<BasicJsonType>::difference_type = long int]'
     reference operator[](difference_type n) const
               ^

以上是在沙箱中完成的

https://wandbox.org/permlink/LNck7Gktm14bmPy0

这不是我正在使用的实际代码,我只想看看我是否可以理解如何使用JSON做各种基本的事情。

目前,我了解得很少,以至于我不知道自己所做的事情本质上是正确的,只是由于愚蠢的事情而中断,或者我所做的事情根本上是错误的。

丹尼尔

nlohmann json库将自己提升为“现代C ++的JSON”,并希望表现得像“就像STL容器一样”。但是,C ++标准库中没有既像矢量一样又像地图一样的容器,并且既不支持值的begin / end迭代器,也不支持键/值对的begin / end迭代器。因此,需要一些新的东西。

nlohmann的原始解决方案是复制jsoncpp的方法,该方法支持json数组的begin / end迭代器,并为迭代器添加了明显不标准的key()功能,以也支持json对象。所以你可以写

for (auto it = RecentFiles.begin(); it != RecentFiles.end(); ++it)
{
    std::cout << it.key() << "\n";
    std::cout << (*it)["Name"].get<std::string>() << "\n";
    std::cout << (*it)["Last modified"].get<std::string>() << "\n";
}

但是,这是一种遍历键/值的非标准方法,它不具有对基于范围的键/值循环的标准库支持。

后来json::items()nlohmann添加了该功能,功能确实支持使用标准迭代器在json对象上进行迭代,并且该功能对基于范围的循环(即viz)具有标准库支持。

int main()
{
    json RecentFiles;

    RecentFiles["1"]["Name"] = "test1.txt";
    RecentFiles["1"]["Last modified"] = "monday";
    RecentFiles["1"]["Score"] = 5.0f;

    RecentFiles["2"]["Name"] = "test2.txt";
    RecentFiles["2"]["Last modified"] = "tuesday";
    RecentFiles["2"]["Score"] = 5.0f;

    for (const auto& item : RecentFiles.items())
    {
        std::cout << item.key() << "\n";
        for (const auto& val : item.value().items())
        {
            std::cout << "  " << val.key() << ": " << val.value() << "\n";
        }
    }
    std::cout << "\nor\n\n";
    for (const auto& item : RecentFiles.items())
    {
        std::cout << item.key() << "\n";
        std::cout << "  " << item.value()["Name"].get<std::string>() << "\n";
        std::cout << "  " << item.value()["Last modified"].get<std::string>() << "\n";
        std::cout << "  " << item.value()["Score"].get<double>() << "\n";
    }
}

输出:

1
  Last modified: "monday"
  Name: "test1.txt"
  Score: 5.0
2
  Last modified: "tuesday"
  Name: "test2.txt"
  Score: 5.0

or

1
  test1.txt
  monday
  5
2
  test2.txt
  tuesday
  5

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章