C:用nlohmann json从文件中读取json对象

前端之家收集整理的这篇文章主要介绍了C:用nlohmann json从文件中读取json对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用nlohmann的json库来处理c中的json对象.最后,我想从文件中读取一个json对象,例如像这样的简单对象.
  1. {
  2. "happy": true,"pi": 3.141
  3. }

我不太清楚如何处理这个问题.在https://github.com/nlohmann,有几种方法可以从字符串文字中反序列化,但是将它扩展为读入文件似乎并不容易.有任何人对此有经验吗?

解决方法

更新2017-07-03 for JSON for Modern C++ version 3

从版本3.0开始,不推荐使用json :: json(std :: ifstream&).一个人应该使用json::parse()代替:

  1. std::ifstream ifs("{\"json\": true}");
  2. json j = json::parse(ifs);

现代C版2的JSON更新

从版本2.0,json::operator>>() id deprecated开始.应该使用json :: json()代替:

  1. std::ifstream ifs("{\"json\": true}");
  2. json j(ifs);

现代C版1的JSON的原始答案

使用json :: operator>>(std :: istream&):

  1. json j;
  2. std::ifstream ifs("{\"json\": true}");
  3. ifs >> j;

猜你在找的C&C++相关文章