2016-04-26 58 views

回答

1

爲了創建一個簡單的例子,讓我們假設我們在代碼的字符串來表示你要接收來自網絡具有以下內容的字符串:

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

這樣解釋的代碼作爲一個字符串是以下情況:

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

int main() 
{ 
    std::stringstream buffer("{ \"Test\": \"string\", \"Test2\": { \"inner0\": \"string2\", \"inner1\": \"string3\", \"inner2\": \"1234\" } }"); 
    std::cout << buffer.str() << std::endl; 

    boost::property_tree::ptree pt; 
    boost::property_tree::json_parser::read_json(buffer, pt); 

    std::string test2_inner0_str = pt.get<std::string>("Test2.inner0"); 
    int test2_inner2_value = pt.get<int>("Test2.inner2"); 

    std::cout << test2_inner0_str << std::endl; 
    std::cout << test2_inner2_value << std::endl; 
} 

打印:

Live On Coliru

{ "Test": "string", "Test2": { "inner0": "string2", "inner1": "string3", "inner2": "1234" } } 
string2 
1234 
+0

優秀的樣本。 +1 – sehe

相關問題