Skip to content

Instantly share code, notes, and snippets.

@BruceChen7
Last active January 31, 2019 07:46
Show Gist options
  • Save BruceChen7/17e6a8583991b7ce23ee3256cb5ff22e to your computer and use it in GitHub Desktop.
Save BruceChen7/17e6a8583991b7ce23ee3256cb5ff22e to your computer and use it in GitHub Desktop.
[#rapidjson#jsoncpp ]#cpp #json
#include <fstream>
#include <cassert>
#include "json/json.h"
// json文件的读取
int main() {
ifstream ifs;
// 打开数据文件
ifs.open("testjson.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
return -1;
}
std::string name = root["name"].asString();
int age = root["age"].asInt();
std::cout<<name<<std::endl;
std::cout<<age<<std::endl;
return 0;
}
// json文件的解析
#pragma comment(lib, "json_mtd.lib")
#include <fstream>
#include <cassert>
#include "json/json.h"
int main() {
ifstream ifs;
ifs.open("testjson.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
// 解析成功返回false
if (!reader.parse(ifs, root, false)) {
return -1;
}
std::string name = root["name"].asString();
int age = root["age"].asInt();
std::cout<<name<<std::endl;
std::cout<<age<<std::endl;
return 0;
}
// json文件的写入
#include <fstream>
#include <cassert>
#include "json/json.h"
int main() {
Json::Value root;
Json::FastWriter writer;
Json::Value person;
person["name"] = "hello world";
person["age"] = 100;
root.append(person);
std::string json_file = writer.write(root);
ofstream ofs;
ofs.open("test1.json");
assert(ofs.is_open());
ofs<<json_file;
return 0;
}
// 序列化操作
/*
{
"array" : [
{
"key" : 0
},
{
"key" : 1
},
{
"key" : 2
},
{
"key" : 3
},
{
"key" : 4
},
{
"key" : 5
},
{
"key" : 6
},
{
"key" : 7
},
{
"key" : 8
},
{
"key" : 9
}
],
"key1" : "value1",
"key2" : "value2"
}
*/
#include <iostream>
#include <string>
#include "json/json.h"
int main(void)
{
Json::Value root;
Json::Value arrayObj;
Json::Value item;
for (int i = 0; i < 10; i ++)
{
item["key"] = i;
arrayObj.append(item);
}
root["key1"] = "value1";
root["key2"] = "value2";
root["array"] = arrayObj;
//root.toStyledString();
std::string out = root.toStyledString();
std::cout << out << std::endl;
return 0;
}
// 获取所有的成员
Json::Value::Members member; // Members 这玩意就是vector<string>,typedef了而已
for (Json::Value::iterator itr = objArray.begin(); itr != objArray.end(); itr++)
{
member = (*itr).getMemberNames();
for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); iter++)
{
string str_temp = (*itr)[(*iter)].asString();
}
}
rapidjson::Document document;
document.Parse(json_param_str);
if (document.HasParseError() || !document.IsObject()) {
return false;
}
rapidjson::Value::ConstMemberIterator iter = document.FindMember("data_names");
if (iter == document.MemberEnd() || !iter->value.IsString()) {
return false;
}
request_param->table_name = iter->value.GetString();
if (!IsTableNameValid(request_param->table_name)) {
return false;
}
// response_format可选
// 0 表示返回json格式的数据,1 表示返回类似于cdn上game detail.js上的数据
// 默认为0
iter = document.FindMember("response_format");
if (iter != document.MemberEnd() && iter->value.IsInt()) {
int32_t response_format = iter->value.GetInt();
request_param->is_response_type_javascript = (response_format == 1);
}
void ErrorResponse(HttpResponse* response, uint32_t error_code, const std::string& error_message) {
// TODO(vectorfeng): 不需要使用rapidjson
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
writer.StartObject();
writer.String("result");
writer.StartObject();
writer.String("error_code");
writer.Int64(error_code);
writer.String("error_message");
writer.String(error_message);
writer.EndObject();
writer.EndObject();
response->mutable_http_body()->assign(sb.GetString(), sb.GetLength());
response->set_status(HttpResponse::Status_OK);
response->SetHeader(kHeaderContentType, kHeaderContentTypeJsonValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment