字符串写入到json文件
背景: 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,那就可以直接解释成json了
int write_notice_to_txt(string str_path, const string& str_content)
{
ofstream ofs;
ofs.open(str_path.c_str(), ofstream::out);
if (ofs.is_open())
{
ofs << str_content;
ofs.close();
return RESULT_OK;
}
return RESULT_ERROR;
}
字符串写入到json文件的更多相关文章
- LitJson(读Exce文件写入到json文件):
读Exce文件写入到json文件汇总: //命名空间 using System.Collections; using System.Collections.Generic; using System. ...
- node——将用户提交的数据写入data.json文件
前续 当我们在进行将数据提交到某个网页时,需要将提交数据保存下来 1.提交数据 2.获得数据 3.保存数据 先看提交数据: <!DOCTYPE html> <html lang=&q ...
- Node.js读取某个目录下的所有文件夹名字并将其写入到json文件
针对解决的问题是,有些时候我们需要读取某个文件并将其写入到对应的json文件(xml文件也行,不过目前用json很多,json是主流). 源码如下:index.js var fs = require( ...
- java的io操作(将字符串写入到txt文件中)
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...
- Angular 通过 $http.post 写入本地 JSON 文件
最近在练习使用 Angular,在实现 $http 对本地 JSON 文档读写的时候遇到了问题. 问题 使用 GET 方法成功将 JSON 文档的内容读出来:但是在使用 POST 插入本地 JSON ...
- nodejs写入json文件,格式化输出json的方法
假如我需要把data序列化成json字符串,然后写入data.json文件中,代码如下: let str = JSON.stringify(data) fs.writeFile('data.json' ...
- 小白学开发(iOS)OC_ 字符串写入文件(2015-08-13)
// // main.m // 字符串写入文件 // // Created by admin on 15/8/13. // Copyright (c) 2015年 admin. All rig ...
- npm package.json文件解读
每个Nodejs项目的根目录下面,一般都会有一个package.json文件.该文件可以由npm init生成,定义了项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据). pa ...
- node.js 中的package.json文件怎么创建?
最近在用webstorm和nodejs做一些东西,老是各种混乱,今天上午创建一个新的项目,结果发现,npm init之后,并没有出现package.json,并没有太明确他的功能的小姑娘表示十分的惊慌 ...
随机推荐
- Go语言关于Type Assertions的疑问
我在"The Go Programming Language Specification"中读到了关于x.(T)这样的语法可以对变量是否符合某一type或interface进行判断 ...
- 定时备份etc目录
#!/bin/bash # #******************************************************************** #encoding -*-utf ...
- iphone bandwidth
iPhone 8, 8 Plus, X peak throughput of ~24GBs iPhone XS, XS Max, XR peak throughput of ~34GBs 在iphon ...
- sql server 遍历表成一棵树结构
一棵树的层次结构都在一张表内,当有这样的需要的时候.. 可以这样玩: <!-- DepartmentDTO 对象对应 department表_查询sql --> <sql id=&q ...
- django.core.exceptions.ImproperlyConfigured: The included URLconf 's9luffycity.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused
出现问题: $ python manage.py runserver 启动项目报错时候 raise ImproperlyConfigured(msg.format(name=self.urlconf_ ...
- SQL Server孤立用戶
如何解决孤立用户问题 http://blog.csdn.net/zzl1120/article/details/7394468 SQL SERVER孤立用户问题解决方法 http://www.2cto ...
- linux系统编程--进程间通信
IPC方法 Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间.任何一个进程的全局变量在另一个进程中都看不到,所以进程和进程之间不能相互访问, 要交换数据必须通过内核,在内核中开 ...
- [Luogu] 计算系数
https://www.luogu.org/problemnew/show/P1313#sub Answer = a ^ n * b ^ m * C(k, min(n, m)) 这里用费马小定理求逆 ...
- IDT系列:(一)初探IDT,Interrupt Descriptor Table,中断描述符表
原文: IDT系列:(一)初探IDT,Interrupt Descriptor Table,中断描述符表 IDT,Interrupt Descriptor Table,中断描述符表是CPU用来处理中 ...
- codeforces316E3
Summer Homework CodeForces - 316E3 By the age of three Smart Beaver mastered all arithmetic operatio ...