我正在try 使用nlohmann json库将json文件解析为json对象.
这是json文件:
{
"n":1,
"data":
{
"name":"Chrome",
"description":"Browse the internet.",
"isEnabled":true
}
}
这是我的代码:
#include <nlohmann/json.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using json = nlohmann::json;
int main()
{
ifstream f("/Users/callum/sfucourses/cmpt373/test/example2.json");
json data = json::parse(f);
cout << data << endl;
}
如果我不进行解析,而只是执行cout << f.rdbuf()
,我会得到正确的输出:
./interpret
/Users/callum/sfucourses/cmpt373/build
{
"n":1,
"data":
{
"name":"Chrome",
"description":"Browse the internet.",
"isEnabled":true
}
}
但如果我进行解析并打印出json对象‘data’,那么"n":1和"name":"Chrome"将放在末尾而不是开头:
./interpret
{"data":{"description":"Browse the internet.","isEnabled":true,"name":"Chrome"},"n":1}
我怎么才能按正确的顺序打印呢?