在c++中使用json-spirit读取json字符串
Using json-spirit to read json string in C++
如何在c++中使用json-spirit读取json字符串?我读了演示代码。我发现:
const Address addrs[5] = { { 42, "East Street", "Newtown", "Essex", "England" },
{ 1, "West Street", "Hull", "Yorkshire", "England" },
{ 12, "South Road", "Aberystwyth", "Dyfed", "Wales" },
{ 45, "North Road", "Paignton", "Devon", "England" },
{ 78, "Upper Street", "Ware", "Hertfordshire", "England" } };
我可以将字符串转换为json对象吗?
char* jsonStr = "{'name', 'Tom'}";
json_spirit提供bool read_string( const String_type& s, Value_type& value )
和bool read( const std::string& s, Value& value )
从字符串中读取json数据。
下面是一个例子:
string name;
string jsonStr("{"name":"Tom"}");
json_spirit::Value val;
auto success = json_spirit::read_string(jsonStr, val);
if (success) {
auto jsonObject = val.get_obj();
for (auto entry : jsonObject) {
if (entry.name_ == "name" && entry.value_.type() == json_spirit::Value_type::str_type) {
name = entry.value_.get_str();
break;
}
}
}
你也可以用ifstream代替string从file中读取json。
请注意,根据RFC4627,字符串以引号开始和结束。
相关文章:
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- 使用 QT 逐行读取 JSON
- 使用 jsoncpp 读取 JSON 文件
- 如何使用 rapidjson 读取 json 文件并输出到 std::string?
- 如何按C++读取 JSON 内容?
- ESP8266读取 JSON,但不读取 PHP 文件
- 使用 boost::asio 读取 JSON 流,获取完整字符串
- 使用Boost Property_tree读取JSON
- c++ jsoncpp 读取 json 文件
- C Nlohmann/JSON如何使用运行时提供JSON_POINTERS读取JSON值
- 使用Boost Json读取JSON消息
- 使用 C++ REST SDK (卡萨布兰卡) 从文件中读取 JSON
- 使用 C++ 和 BOOST 读取 JSON 文件
- 分段错误读取 JSON 文件
- 如何在C++中读取.json文件
- C++:使用nlohmann json从文件中读取json对象
- JSON Spirit读取JSON数组
- 在c++中使用json-spirit读取json字符串
- 使用boost::asio从套接字读取JSON
- 用c++读取json文件