C++ 我试图打开一个json文件。并将其存储为json对象。
我试图打开一个具有json扩展名的文件,并将其存储为对象。但是,会不断收到一条错误消息,说明未在作用域中声明文件名。我不熟悉使用json文件。您对待它们是否与普通文本文件不同C++ 我试图打开一个json文件。并将其存储为json对象。,c++,json,C++,Json,我试图打开一个具有json扩展名的文件,并将其存储为对象。但是,会不断收到一条错误消息,说明未在作用域中声明文件名。我不熟悉使用json文件。您对待它们是否与普通文本文件不同 #include "json.hpp" #include <iostream> #include <stdio.h> #include <fstream> #include <string> int main(int argc, char** argv) { std
#include "json.hpp"
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
int main(int argc, char** argv) {
std::ifstream file;
file.open(test.json);
nlohmann::json jsonObject;
// Store the contents filename into jsonObject
if (file.is_open()) {
file >> jsonObject;
}
file.close();
}
#包括“json.hpp”
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
std::ifstream文件;
open(test.json);
nlohmann::json jsonObject;
//将内容文件名存储到jsonObject中
if(file.is_open()){
文件>>jsonObject;
}
file.close();
}
您通过了test.json
作为open
-函数的文件名。因此,编译器假定名为test
的对象具有数据成员json
。除非您在代码中定义了这样一个对象,否则编译器将告诉您范围中没有名为test
的对象。这就是为什么
你可能是说
if (file.open("test.json")) {
...
file.open(“test.json”)
“…文件名未在作用域中声明。”与文件为json(或其他任何内容)无关。顺便提一下请务必逐字逐句地发布任何编译器错误。同时请将代码精简到最少的示例,因为它不符合主题。