mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-10-24 20:50:12 +08:00
* update example directory
* modify some compile error.
* update with clang-format
* update
* update
* add_definitions("../include/json")
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Jul 10 21:26:16 2019 +0800
#
# On branch code_example
# Your branch is up-to-date with 'origin/code_example'.
#
# Changes to be committed:
# modified: example/CMakeLists.txt
#
* change CMakeLists.txt
* update streamWrite.cpp
* update
* Update readFromStream.cpp
* fix typo
38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
#include "json/json.h"
|
|
#include <iostream>
|
|
/**
|
|
* \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 int 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" << 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;
|
|
}
|