【发布时间】:2022-11-18 02:21:16
【问题描述】:
我必须将 JSON 转换为原始数据,然后将其写入文件。我找到了以下示例:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
std::cout << j.dump() << std::endl;
//convert json to string
std::string s = j.dump ();
//convert from string to stream
const char *pData = s.c_str();
//convert from stream to string
std::string out(pData );
}
还是不知道反方向怎么做。
【问题讨论】:
-
当问题是关于编译器错误时,您应该在问题中包括编译器错误。如果您发布其他人编写的代码,您应该提供对源代码的引用
-
你是什么意思原始数据?
j.dump()返回一个字符串。那是您想保存到文件中的内容吗?就我个人而言,我不会将字符串称为原始数据,但也许您的想法不同。 -
@k314159 缺少括号似乎是编译错误的原因
-
你读过documentation了吗?
-
为什么是
fopen? std::ofstream 出了什么问题