#include "json/json.h"#include <iostream>#include <memory>/*** \brief Parse a raw string into Value object using the CharReaderBuilder* class, or the legacy Reader class.* Example Usage:* $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString* $./readFromString* colin* 20*/int main() {const std::string rawJson = R"({"Age": 20, "Name": "colin"})";const auto rawJsonLength = static_cast<int>(rawJson.length());constexpr bool shouldUseOldWay = false;JSONCPP_STRING err;Json::Value root;if (shouldUseOldWay) {Json::Reader reader;reader.parse(rawJson, root);} else {Json::CharReaderBuilder builder;const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,&err)) {std::cout << "error: " << err << std::endl;return EXIT_FAILURE;}}const std::string name = root["Name"].asString();const int age = root["Age"].asInt();std::cout << name << std::endl;std::cout << age << std::endl;return EXIT_SUCCESS;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。