rapidjson串组装的代码示例
2017-09-30 20:15
411 查看
我们知道json串的格式, 那么组装json不就很容易吗? 恩, 但是, 如果我们自己组装, 遇到特殊字符会有坑, 而且, 代码看起来恶心, 不信? 来看看:
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的rapidjson
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
string strPath = "C:\\a.txt";
string strJson = "{\"path\":\"" + strPath + "\"}";
cout << strJson << endl;
}
int main(int argc, char *argv[])
{
test();
return 0;
} 结果: {"path":"C:\a.txt"}
你以为这是你预期的结果吗? 用json解析器检验一下, 就发下上面的json串是错误的, 来看看rapidjson组装方法:
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的rapidjson
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
string strPath = "C:\\a.txt";
Document document;
Document::AllocatorType& allocator = document.GetAllocator();
Value root(kObjectType);
Value name(kStringType);
name.SetString(strPath.c_str(), allocator);
root.AddMember("pash", name, allocator);
root.AddMember("id", 123, allocator);
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
root.Accept(writer);
string reststring = buffer.GetString();
cout << reststring << endl;
}
int main(int argc, char *argv[])
{
test();
return 0;
} 结果:{"pash":"C:\\a.txt","id":123}
这才是预期中的json串吗, 搞定。 自己组装json串是流氓行为, 尽管, 我偶尔也这么组装简单的字符串。
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的rapidjson
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
string strPath = "C:\\a.txt";
string strJson = "{\"path\":\"" + strPath + "\"}";
cout << strJson << endl;
}
int main(int argc, char *argv[])
{
test();
return 0;
} 结果: {"path":"C:\a.txt"}
你以为这是你预期的结果吗? 用json解析器检验一下, 就发下上面的json串是错误的, 来看看rapidjson组装方法:
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的rapidjson
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
string strPath = "C:\\a.txt";
Document document;
Document::AllocatorType& allocator = document.GetAllocator();
Value root(kObjectType);
Value name(kStringType);
name.SetString(strPath.c_str(), allocator);
root.AddMember("pash", name, allocator);
root.AddMember("id", 123, allocator);
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
root.Accept(writer);
string reststring = buffer.GetString();
cout << reststring << endl;
}
int main(int argc, char *argv[])
{
test();
return 0;
} 结果:{"pash":"C:\\a.txt","id":123}
这才是预期中的json串吗, 搞定。 自己组装json串是流氓行为, 尽管, 我偶尔也这么组装简单的字符串。
相关文章推荐
- json字符串之间的相互转换示例代码
- 使用$.getJSON实现跨域ajax请求示例代码
- RapidJSON 代码剖析(二):使用 SSE4.2 优化字符串扫描
- asp.net后台cs中的JSON格式变量在前台Js中调用方法(前后台示例代码)
- 使用$.getJSON实现跨域ajax请求示例代码
- 利用js实现前后台传送Json的示例代码
- SpringBoot返回json和xml的示例代码
- php格式化json函数示例代码
- asp.net后台cs中的JSON格式变量在前台Js中调用方法(前后台示例代码)
- Android 解析JSON数据示例代码[CODE]
- Js和Jquery中ajax返回JSON格式的两种方法!(各有示例代码)
- springmvc 返回json数据给移动端,配置及代码示例
- rapidjson解析json代码实例以及常见的json core dump问题
- json-c代码示例
- ajax后台处理返回json值示例代码
- 代码组装Json字符串
- JQuery以JSON方式提交数据到服务端示例代码
- Json技术使用代码示例
- rapidjson组装继续简化------人生苦短,我用rapidjson
- cocos2d-x使用libjson和tinyxml解析json和xml的代码示例