Ubuntu 20.04
IDE Clion
目录
内容如下:
- {
- "date": "2022.8.31",
- "name": "Rose",
- "age": 24,
- "Number": 100
- }
- #include
- #include
- #include
- #include
-
- using namespace std;
-
- void PasingFile(string file_path, map
& info_map) { - Json::Reader reader;
- Json::Value root;
- ifstream srcFile(file_path, ios::binary);//以二进制方式打开文件
- if (!srcFile.is_open()) //检查文件打开情况
- cout << "error: open file failed" << endl;
-
- if (reader.parse(srcFile, root)) {
- //读取根节点信息
- info_map["date"] = root["date"].asString();
- info_map["Number"] = to_string(root["Number"].asInt());
- info_map["age"] = to_string(root["age"].asInt());
- info_map["name"] = root["name"].asString();
- }
- srcFile.close();
- map
::iterator it; - for ( it= info_map.begin(); it != info_map.end(); it++) {
- cout << it->first << ": " << it->second << endl;
- }
- }
-
- int main() {
- map
info_map_test; - PasingFile("/home/ts/TEst/test.json", info_map_test);
- return 0;
- }
- cmake_minimum_required(VERSION 3.5)
- project(TEst)
- set(CMAKE_CXX_STANDARD 14)
-
- link_directories(${json_lib}/home/ts/jsoncpp-src-0.5.0/libs/linux-gcc-9/)
- include_directories(${JSON_INC_PATH}/home/ts/jsoncpp-src-0.5.0/include/)
- add_executable(TEst main.cpp)
- target_link_libraries(TEst libjson_linux-gcc-9_libmt.so)
- Number: 100
- age: 24
- date: 2022.8.31
- name: Rose
-
- Process finished with exit code 0