使用 boost json ptree 时如何检查空值
How to check a value for null when using boost json ptree
我从服务器收到以下格式的 json 响应:
{"value": 98.3}
但是在某些情况下,响应可以是:
{"value": null}
我已经编写了一个使用 boost json 的 C++ 程序来解析它并获得 float
的值
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
namespace pt = boost::property_tree;
int main(int argc, char **argv)
{
try
{
float valuef;
pt::ptree root;
std::stringstream ss;
string result;
std::ifstream file("test.json");
if (file)
{
ss << file.rdbuf();
file.close();
}
pt::read_json(ss, root);
auto value = root.get<string>("value");
if (value != "null")
{
valuef = stof(value);
}
cout <<"float value is" << valuef << endl;
}
catch (std::exception &e)
{
string err = e.what();
cout << "error is " << endl
<< err << endl;
}
}
所以我总是检查该值是否不等于文字“null”,因为根据这个
Boost Json with null elements
不支持输出空值。
由于 post 已经 7 岁了,我想知道最新的 boost 库是否支持更通用的东西来检查空值?
没有。来自 the documentation:
JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
使用boost/property_tree/json_parser.hpp
你只能像你说的那样检查字符串版本:
So I always check if the value is not equal with the literal "null" because according to this
Boost Json with null elements outputting nulls is not supported.
但是,自 boost version 1.75.0 起,您还可以使用 boost/json.hpp
来处理 JSON 个对象。
已解析的 JSON 对象现在属于 json::value
类型,并且类型由 json::kind
指定。参见 boost.org's example。
因此,使用 value 您可以执行以下操作:
// jv is a value from a kvp-pair
if(jv.is_null()) {
// Json value is null
}
具体实现请参考 boost 示例。
我从服务器收到以下格式的 json 响应:
{"value": 98.3}
但是在某些情况下,响应可以是:
{"value": null}
我已经编写了一个使用 boost json 的 C++ 程序来解析它并获得 float
的值#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
namespace pt = boost::property_tree;
int main(int argc, char **argv)
{
try
{
float valuef;
pt::ptree root;
std::stringstream ss;
string result;
std::ifstream file("test.json");
if (file)
{
ss << file.rdbuf();
file.close();
}
pt::read_json(ss, root);
auto value = root.get<string>("value");
if (value != "null")
{
valuef = stof(value);
}
cout <<"float value is" << valuef << endl;
}
catch (std::exception &e)
{
string err = e.what();
cout << "error is " << endl
<< err << endl;
}
}
所以我总是检查该值是否不等于文字“null”,因为根据这个
Boost Json with null elements
不支持输出空值。
由于 post 已经 7 岁了,我想知道最新的 boost 库是否支持更通用的东西来检查空值?
没有。来自 the documentation:
JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
使用boost/property_tree/json_parser.hpp
你只能像你说的那样检查字符串版本:
So I always check if the value is not equal with the literal "null" because according to this Boost Json with null elements outputting nulls is not supported.
但是,自 boost version 1.75.0 起,您还可以使用 boost/json.hpp
来处理 JSON 个对象。
已解析的 JSON 对象现在属于 json::value
类型,并且类型由 json::kind
指定。参见 boost.org's example。
因此,使用 value 您可以执行以下操作:
// jv is a value from a kvp-pair
if(jv.is_null()) {
// Json value is null
}
具体实现请参考 boost 示例。