Reading json files in C++

前端 未结 7 2071
梦如初夏
梦如初夏 2021-02-01 03:17

I\'m trying to read in a JSON file. So far I have focused on using the jsoncpp library. However, the documentation is quite hard to understand for me. Could anyone

相关标签:
7条回答
  • 2021-02-01 04:16

    Example (with complete source code) to read a json configuration file:

    https://github.com/sksodhi/CodeNuggets/tree/master/json/config_read

     > pwd
    /root/CodeNuggets/json/config_read
     > ls
    Makefile  README.md  ReadJsonCfg.cpp  cfg.json
     > cat cfg.json 
    {
       "Note" : "This is a cofiguration file",
       "Config" : { 
           "server-ip"     : "10.10.10.20",
           "server-port"   : "5555",
           "buffer-length" : 5000
       }   
    }
     > cat ReadJsonCfg.cpp 
    #include <iostream>
    #include <json/value.h>
    #include <jsoncpp/json/json.h>
    #include <fstream>
    
    void 
    displayCfg(const Json::Value &cfg_root);
    
    int
    main()
    {
        Json::Reader reader;
        Json::Value cfg_root;
        std::ifstream cfgfile("cfg.json");
        cfgfile >> cfg_root;
    
        std::cout << "______ cfg_root : start ______" << std::endl;
        std::cout << cfg_root << std::endl;
        std::cout << "______ cfg_root : end ________" << std::endl;
    
        displayCfg(cfg_root);
    }       
    
    void 
    displayCfg(const Json::Value &cfg_root)
    {
        std::string serverIP = cfg_root["Config"]["server-ip"].asString();
        std::string serverPort = cfg_root["Config"]["server-port"].asString();
        unsigned int bufferLen = cfg_root["Config"]["buffer-length"].asUInt();
    
        std::cout << "______ Configuration ______" << std::endl;
        std::cout << "server-ip     :" << serverIP << std::endl;
        std::cout << "server-port   :" << serverPort << std::endl;
        std::cout << "buffer-length :" << bufferLen<< std::endl;
    }
     > cat Makefile 
    CXX = g++
    PROG = readjsoncfg
    
    CXXFLAGS += -g -O0 -std=c++11
    
    CPPFLAGS += \
            -I. \
            -I/usr/include/jsoncpp
    
    LDLIBS = \
                     -ljsoncpp
    
    LDFLAGS += -L/usr/local/lib $(LDLIBS)
    
    all: $(PROG)
            @echo $(PROG) compilation success!
    
    SRCS = \
            ReadJsonCfg.cpp
    OBJS=$(subst .cc,.o, $(subst .cpp,.o, $(SRCS)))
    
    $(PROG): $(OBJS)
            $(CXX) $^ $(LDFLAGS) -o $@
    
    clean:
            rm -f $(OBJS) $(PROG) ./.depend
    
    depend: .depend
    
    .depend: $(SRCS)
            rm -f ./.depend
            $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MM $^ >  ./.depend;
    
    include .depend
     > make
    Makefile:43: .depend: No such file or directory
    rm -f ./.depend
    g++ -g -O0 -std=c++11 -I. -I/usr/include/jsoncpp -MM ReadJsonCfg.cpp >  ./.depend;
    g++ -g -O0 -std=c++11 -I. -I/usr/include/jsoncpp  -c -o ReadJsonCfg.o ReadJsonCfg.cpp
    g++ ReadJsonCfg.o -L/usr/local/lib -ljsoncpp -o readjsoncfg
    readjsoncfg compilation success!
     > ./readjsoncfg 
    ______ cfg_root : start ______
    {
            "Config" : 
            {
                    "buffer-length" : 5000,
                    "server-ip" : "10.10.10.20",
                    "server-port" : "5555"
            },
            "Note" : "This is a cofiguration file"
    }
    ______ cfg_root : end ________
    ______ Configuration ______
    server-ip     :10.10.10.20
    server-port   :5555
    buffer-length :5000
     > 
    
    0 讨论(0)
提交回复
热议问题