jsoncpp操作 json

#include <iostream>

//#include "json/json.h"

#include "json_reader.cpp"
#include "json_value.cpp"
#include "json_writer.cpp"

using namespace std;
using namespace Json;

int main(int argc, char *argv[])
{
    
    // Configuration options
    char *config_doc=" { \"encoding\" :\"UTF-8\",\"plug-ins\" : [\"python\",\"c++\",\"ruby\"],\"indent\" : { \"length\" : 3, \"use_space\": true }}";

    Json::Value root;   // will contains the root value after parsing.
    Json::Reader reader;
    bool parsingSuccessful = reader.parse( config_doc, root );
    if ( !parsingSuccessful )
    {
        // report to the user the failure and their locations in the document.
        std::cout  << "Failed to parse configuration\n"
                   << reader.getFormattedErrorMessages();
        return 1;
    }

    reader.parse(config_doc,root,false);     //解析出json放到json中
    
    std::string encoding = root.get("encoding", "GBK" ).asString();
    const Json::Value plugins = root["plug-ins"];
    
    for(int index = 0; index < plugins.size(); ++index ){
            cout<<plugins[index];
    }
    
    cout<< plugins;
    cout<<encoding<<endl;
    
    root["encoding"] = "GB2312";
    root["indent"]["length"] =  5;
    root["indent"]["use_space"] = false;
                 
    Json::StyledWriter writer;
    
    std::string outputConfig = writer.write( root ); 
    
    cout<<outputConfig;
                                    
    system("PAUSE");
    return 0;
}
 

 

你可能感兴趣的:(json)