Jsoncpp example

前端之家收集整理的这篇文章主要介绍了Jsoncpp example前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载自json c++ examples

// ---- create from scratch ----

Json::Value fromScratch@H_502_6@;
Json::Value array@H_502_6@;
array.append("hello")@H_502_6@;
array.append("world")@H_502_6@;
fromScratch["hello"] = "world"@H_502_6@;
fromScratch["number"] = 2@H_502_6@;
fromScratch["array"] = array@H_502_6@;
fromScratch["object"]["hello"] = "world"@H_502_6@;

output(fromScratch)@H_502_6@;

// write in a nice readible way
Json::StyledWriter styledWriter@H_502_6@;
std::cout << styledWriter.write(fromScratch)@H_502_6@;

// ---- parse from string ----

// write in a compact way
Json::FastWriter fastWriter@H_502_6@;
std::string jsonMessage = fastWriter.write(fromScratch)@H_502_6@;

Json::Value parsedFromString@H_502_6@;
Json::Reader reader@H_502_6@;
bool parsingSuccessful = reader.parse(jsonMessage,parsedFromString)@H_502_6@;
if (parsingSuccessful)
{
    std::cout << styledWriter.write(parsedFromString) << std::endl@H_502_6@;
}
void output(const Json::Value & value)
{
    @H_502_6@// querying the json object is very simple
    std::cout << value["hello"];
    std::cout << value["number"];
    std::cout << value["array"][0] << value["array"][1];
    std::cout << value["object"]["hello"];
}

猜你在找的Json相关文章