Jsoncpp example

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

转载自json c++ examples

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

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

output(fromScratch)@H_404_6@;

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

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

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

Json::Value parsedFromString@H_404_6@;
Json::Reader reader@H_404_6@;
bool parsingSuccessful = reader.parse(jsonMessage,parsedFromString)@H_404_6@;
if (parsingSuccessful)
{
    std::cout << styledWriter.write(parsedFromString) << std::endl@H_404_6@;
}
void output(const Json::Value & value)
{
    @H_404_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"];
}
原文链接:https://www.f2er.com/json/289475.html

猜你在找的Json相关文章