首页 > Web开发 > 详细

字符串写入到json文件

时间:2018-01-13 17:47:55      阅读:245      评论:0      收藏:0      [点我收藏+]

背景: PHP产生公告 ,发送到CGI ,在CGI把该公告的json 字符串写入到文件内(转义后的字符串)

通过 jsoncpp 操作

    int write_notice_to_json(string str_path, const string& str_content)
    {  
        Json::Reader reader;
        Json::FastWriter writer;
        Json::Value root;
        if (false == reader.parse(str_content, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素
        {
            return  RESULT_ERROR; 
        } 

        std::string json_file = writer.write(root);
        ofstream ofs;
        ofs.open(str_path.c_str(), ofstream::out);
        if (ofs.is_open())
        {
            ofs << json_file;
            ofs.close(); 
            return  RESULT_OK;
        } 
        return  RESULT_ERROR;
    }  

今天来到公司终于搞定了  mark一下

 

收到的字符串内容:

{\"Notice\":{\"NoticeVersion\":\"1414\",\"noticeContent\":[{\"Image\":\"notice\\/notice01\",\"ImageWidth\":\"350\",\"ImageHeight\":\"5\"},{\"Image\":\"notice\\/notice02\",\"ImageWidth\":\"350\",\"ImageHeight\":\"5\"},\"jghjfghjfghj\"]}}

存到文件后

技术分享图片

 

 

踩的坑:一开始想通过C++ 替换掉转义字符串 但是不起作用

 CStringUtils::Replace(m_str_content, "\\\"", "\"");

 

字符串写入到json文件

原文:https://www.cnblogs.com/Jimmy104/p/8279695.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!