简体   繁体   English

从 MAP 中的 C++ 文件中读取键值对(Json 格式)

[英]Read Key Value(s) Pair(Json Format) from a file in C++ in a MAP

Hi I am new to programming world of C++ (Beginner).嗨,我是 C++ 编程世界的新手(初学者)。 My query is I have a JSON File (Key, Value(s) format), which need to accessed by C++ and store in Map(Key, Value(s)).我的查询是我有一个 JSON 文件(Key, Value(s) 格式),需要通过 C++ 访问并存储在 Map(Key, Value(s)) 中。

JSON File: JSON 文件:

{
    "1": [
        4
    ],
    "2": [
        3
    ],
    "3": [
        1
    ],
    "4": [
        2,
        3,
        4,
        5,
        6
    ],
    "5": [
        5
    ],
    "6": [
        3
    ]
    "7": [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10
    ]
}

1) Get the json library( https://sourceforge.net/projects/jsoncpp/ ) 2) Extract it. 1) 获取 json 库 ( https://sourceforge.net/projects/jsoncpp/ ) 2) 提取它。

#include <bits/stdc++.h>
#include "json_cpp-master/json/json.h"
#include "json_cpp-master/json/reader.h"
#include "json_cpp-master/json/value.h"
using namespace std;
int main(){
    Json::Value root;
    Json::CharReaderBuilder reader;

    ifstream file("file.json");
    file >> root;

    cout << root[2][1];
    cout << root;
    return 0;
}

3)Then Do the following:- sudo apt-get install libjsoncpp-dev and sudo ln -s /usr/include/jsoncpp/json/ /usr/include/json 3)然后执行以下操作:- sudo apt-get install libjsoncpp-dev 和 sudo ln -s /usr/include/jsoncpp/json/ /usr/include/json

4)Run the program :- g++ program.cpp -ljsoncpp -o program 4)运行程序:- g++ program.cpp -ljsoncpp -o program

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM