2016-04-26 4 views

Antwort

5

Hier ist ein Beispielcode für diese Aufgabe zu tun:

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/json_parser.hpp> 
#include <boost/optional.hpp> 
#include <iostream> 
#include <sstream> 
#include <cstdlib> 

int main() 
{ 
    boost::property_tree::ptree pt; 
    pt.put("Test", "string"); 
    pt.put("Test2.inner0", "string2"); 
    pt.put("Test2.inner1", "string3"); 
    pt.put("Test2.inner2", 1234); 

    std::stringstream ss; 
    boost::property_tree::json_parser::write_json(ss, pt); 

    std::cout << ss.str() << std::endl; 

    return 0; 
} 

Um diesen Code mit GCC zu kompilieren:

g++ main.cpp -lboost_system -o SamplePT_JSON 

Und hier ist die erwartete Ausgabe:

{ 
    "Test": "string", 
    "Test2": 
    { 
     "inner0": "string2", 
     "inner1": "string3", 
     "inner2": "1234" 
    } 
} 
Verwandte Themen