jsoncpp 中关于浮点数的控制和中文显示问题

在做项目的时候需要使用到jsoncpp这个开源库,但是在使用是发现在处理浮点数和中文时存在显示上的问题;
例1:

//在存储浮点数时
Json::Value flaot;
float["Age"] = 3.15;

则打印出来的就是

{"Age":3.1499999999999999}

例2:

//在打印中文时
Json::Value data;
data["name"] = "张三";

打印出来就是Unicode码了

{"Name":"\u5f20\u4e09"}

解决方法:

int main(int argc, const char *argv[])                                                     
{                                                                                          
   Json::Value root1;                                                                     
   JSONCPP_STRING errs;                                                                   
   Json::CharReaderBuilder readerBuilder;                                                 
                                                                                           
   char content[520] = {0};                                                               
   sprintf(content, "%s", "{\"name\":\"张三\",\"age\":3.15}");                            
                                                                                           
   unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());          
   bool res = jsonReader->parse(content, content+strlen(content), &root1, &errs);         
   if(!res || !errs.empty()) {                                                            
      cout << "parseJson err. " << errs << endl;                                         
   }                                                                                      
                                                                                           
   string name = root1["name"].asString();                                                
   double age = root1["age"].asDouble();                                                                                                                                             
   cout << root1 << endl;                                                                 
/*-------------------------------------------------------------------------------*/
                                                                                           
   string jsonStr;                                                                        
   Json::Value root;                                                                      
   Json::StreamWriterBuilder writerBuilder;                                               
   std::ostringstream os;                                                                 
                                                                                           
   int c = 4;                                                                             
   string str_get;                                                                        
   double get = precision(age, c, str_get);                                               
                                                                                           
   root["Name"] = name;                                                                   
   root["Age"] = get;                                                                                                             
                                                                                           
   writerBuilder.settings_["indentation"] = "";                                           
   writerBuilder.settings_["precision"] = 16;                           
   writerBuilder.settings_["emitUTF8"] = true;    
                                                                                           
   std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());       
   jsonWriter->write(root, &os);                                                          
   jsonStr = os.str();                                                                    
                                                                                           
   cout << (char *)jsonStr.c_str() << endl;                                               
                                                                                           
   return 0;                                                                              
}                                                                                          

writerBuilder.settings_[“precision”] = 16;
该选项表示浮点数能存储的位数的最大个数,包括整数位,默认是17

writerBuilder.settings_[“emitUTF8”] = true;
该选项表示使能字符串UTF8格式显示(默认Unicode)

你可能感兴趣的:(C++,jsoncpp)