环境:VS2010SP1、boost 1.55
strVal = boost::lexical_cast<std::string>(jsonVal.asDouble());
上面语句转成的浮点会有变化,得用下面的语句代替
try { Json::Value jsonVal = record[key.c_str()]; std::string strVal; if (jsonVal.isString()) { strVal = jsonVal.asString(); } else if (jsonVal.isBool()) { strVal = jsonVal.asBool()==true?"true":"false"; } else if (jsonVal.isDouble()) { char szBuf[MAX_PATH] = {0}; sprintf(szBuf,"%lf",jsonVal.asDouble()); strVal = szBuf; } else if (jsonVal.isInt()) { strVal = boost::lexical_cast<std::string>(jsonVal.asInt()); } else if (jsonVal.isUInt()) { strVal = boost::lexical_cast<std::string>(jsonVal.asUInt()); } mapRec[key.c_str()]=strVal; } catch(boost::bad_lexical_cast &e) { }
再测试,问题解决。猜测是boost::Lexical_cast的问题。
原文链接:https://www.f2er.com/json/289874.html