4

当我询问rapidjson::Value使用GetType()方法的类型时,它只返回以下类型:

//! Type of JSON value
enum Type {
    kNullType = 0,      //!< null
    kFalseType = 1,     //!< false
    kTrueType = 2,      //!< true
    kObjectType = 3,    //!< object
    kArrayType = 4,     //!< array
    kStringType = 5,    //!< string
    kNumberType = 6     //!< number
};

如您所见,没有这样的kIntType也不kDoubleType(甚至kUintTypekInt64Type)因此,我无法获得 的实际值rapidjson::Value

例如:

if (value.GetType() == rapidjson::kNumberType)
{
    double v = value.GetDouble()        // this?
    unsigned long v = value.GetUInt64() // or this??
    int v = value.GetInt()              // or this?
}

无论如何要区分实际的数字类型吗?

谢谢。

4

1 回答 1

11

有:

  1. bool Value::IsInt() const
  2. bool Value::IsUint() const
  3. bool Value::IsInt64() const
  4. bool Value::IsUint64() const
  5. bool Value::IsDouble() const

请注意,1-4 不是互斥的。例如,该值123将使 1-4 返回true,但 5 将返回false。当or 1-5 is时调用GetDouble()总是可以的,尽管当该值实际上是 64 位(无符号)整数时可能会丢失精度。IsNumber()true

http://miloyip.github.io/rapidjson/md_doc_tutorial.html#QueryNumber

于 2015-05-03T11:57:42.937 回答