jsoncpp之json和string互相转换

Json转string

Json::Value root;
root["map_name"] = "test";
root["position_name"] = "test1";

Json::StreamWriterBuilder writerBuilder;
std::unique_ptr<Json::StreamWriter> json_write(writerBuilder.newStreamWriter());
std::ostringstream ss;
json_write->write(root, &ss);
std::string strContent = ss.str();
std::cout << strContent << std::endl;

string转Json

string laser = "{\"data\":[{\"createdAt\":\"2016-08-11 04:08:30\",\"dataFileName\":\"40dd8fcd-5e6d-4890-b620-88882d9d3977.data\",\"id\":0,\"mapInfo\":{\"gridHeight\":992,\"gridWidth\":992,\"originX\":-24.8,\"originY\":-24.8,\"resolution\":0.05000000074505806},\"name\":\"demo\",\"status\":4}],\"successed\":true}";
Json::Value tree;
Json::String err;
Json::CharReaderBuilder reader;
std::unique_ptr<Json::CharReader>const json_read(reader.newCharReader());
json_read->parse(laser.c_str(), laser.c_str() + laser.length(), &tree,&err);

你可能感兴趣的:(demo)