An example for how to parse a json file

#1

I like using the forum as note book. Here some code that shows how to parse the content of a json file.

//--------------------------------------------------------------
void ofApp::setup(){
//https://github.com/nlohmann/json
   
        string _fixtureBrand = "UKING";
    string _fixtureName = "RGBW";
    
    int COLOUR_WHEEL_1 = -1;
    
    std::string path = ofToDataPath( "fixtures/"+_fixtureName+".json" );
    
    ofFile file( path );
    if( file.exists() ){
        ofLog() << "found file = "<<path<<", loading";
        ofJson json = ofLoadJson( path );

//        ofLog()<<json.size();
//        ofLog()<<json.back();
        
        string json_string = json.dump();
        ofLog()<<"json_string "<<json_string;

        ofLog << json[_fixtureBrand][_fixtureName]["PAN"];
        
        if(json["COLOUR WHEEL 1"].is_null() == false) COLOUR_WHEEL_1 = json["COLOUR WHEEL 1"];

        
    }else{
        ofLog() << "file not found for state name = "<<path;
    }     
}

My json file looks like this:

1 Like
#2
 void loadJson(){
        ofLog()<<"loadJson() ";
        
        std::string path = ofToDataPath("paths/"+path_filename+".json" );
        ofFile file( path );
        if( file.exists() ){
            ofLog() << "found file = "<<path<<", loading";
            ofJson json = ofLoadJson( path );
            
            ofLog()<<"json has "<<json.size()<<" entries";
            
            for (auto& jj : json) {
                if(jj["mainA"].is_null() == false)  rec_mainAngle.push_back(jj["mainA"]);
                if(jj["panA"].is_null() == false)  rec_panAngle.push_back(jj["panA"]);
                if(jj["tiltA"].is_null() == false)  rec_tiltAngle.push_back(jj["tiltA"]);
                
                if(jj["panCoarsedmx"].is_null() == false)    rec_panCoarseDMX.push_back(jj["panCoarsedmx"]);
                if(jj["panFinedmx"].is_null() == false)    rec_panFineDMX.push_back(jj["panFinedmx"]);
                if(jj["tiltCoarsedmx"].is_null() == false)    rec_tiltCoarseDMX.push_back(jj["tiltCoarsedmx"]);
                if(jj["tiltFinedmx"].is_null() == false)    rec_tiltFineDMX.push_back(jj["tiltFinedmx"]);
                
                
                if(jj["focusCoarsedmx"].is_null() == false)    ofLog()<<"focusCoarsedmx "<<jj["focusCoarsedmx"];
                if(jj["focusFinedmx"].is_null() == false)     ofLog()<<"focusFinedmx "<<jj["focusFinedmx"];
                if(jj["zoomCoarsedmx"].is_null() == false)    ofLog()<<"zoomCoarsedmx "<<jj["zoomCoarsedmx"];
                if(jj["zoomFinedmx"].is_null() == false)   ofLog()<<"zoomFinedmx "<<jj["zoomFinedmx"];
            }
            
        }
    }
    
    void saveJson(){
        
        ofLog()<<"saveJson() ";
        std::string path = ofToDataPath("paths/"+path_filename+".json" );
        
        ofJson json = ofLoadJson(path);
        
        for (int i = 0; i<rec_mainAngle.size(); i++) {
            
            ofJson jj = {
                {"i", i},
                {"mainA", rec_mainAngle[i]},
                {"panA", rec_panAngle[i]},
                {"tiltA", rec_tiltAngle[i]},
                
                
                {"panCoarsedmx", rec_panCoarseDMX[i]},
                {"panFinedmx", rec_panFineDMX[i]},
                {"tiltCoarsedmx", rec_tiltCoarseDMX[i]},
                {"tiltFinedmx", rec_tiltFineDMX[i]},
                //                 {"focusCoarsedmx", -1},
                //                 {"focusFinedmx", -1},
                //                 {"zoomCoarsedmx", -1},
                //                 {"zoomFinedmx", -1},
                
                //                 {"time", ofGetTimestampString()}
            };
            
            //if this json object / angle name already exists it will be overwritten
            json[ofToString(rec_mainAngle[i])] = jj;
        }
        
        
        ofSavePrettyJson(path, json);
    }