分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

字符串写入到json文件

发布时间:2023-09-06 01:36责任编辑:白小东关键词:jsjson

背景: 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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved