#JSON文件
编辑一个json文件,里面包含了对象以及数组等信息,文件内容如下:
{
"ip" : "1.2.3.4",
"port" : "8088",
"operDir" : "/tmp/curl/",
"url" :
[
{
"name":"raid_info",
"method" : "get",
"post_data": ""
},
{
"name":"hello",
"method" : "post",
"post_data":
{
"op":"log_detail",
"type":"shift",
"file_name":"/home/log",
"begin_shift":3,
"lines_count":2000
}
}
]
}
#读取文件
将数据读入到内存:
static std::string read_file(const char *path)
{
FILE *file = fopen(path, "rb");
if ( !file )
return std::string("");
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
std::string text;
char *buffer = new char[size+1];
buffer[size] = 0;
if (fread(buffer, 1, size, file) == (unsigned long)size)
text = buffer;
fclose(file);
delete[] buffer;
return text;
}
#解析数据
将读入的数据全部放入Json::Reader,解析完毕之后数据就已经解析完毕了:
static int
parse_value(const std::string &input)
{
Json::Value root;
const Json::Features features;
Json::Reader reader(features);
if (!reader.parse(input, root))
{
std::cout << "Failed to parse" << std::endl;
return 1;
}
std::cout << "ip:" << root["ip"].asString() << std::endl;
std::cout << "post:" << root["port"].asString() << std::endl;
std::cout << "operDir:" << root["operDir"].asString() << std::endl;
Json::Value interface = root["url"];
Json::FastWriter fastWriter;
for (unsigned int i = 0; i < interface.size(); i++)
{
std::cout << "###############" << std::endl;
std::cout << "name:" << interface[i]["name"].asString() << std::endl;
std::cout << "method:" << interface[i]["method"].asString() << std::endl;
Json::Value post_data = interface[i]["post_data"];
std::cout << "post_data" << post_data << std::endl;
std::string output = fastWriter.write(post_data);
std::cout << "output" << output << std::endl;
}
return 0;
}
#所有源码如下:
#include "json/json.h"
#include <algorithm>
#include <stdio.h>
static std::string read_file(const char *path)
{
FILE *file = fopen(path, "rb");
if ( !file )
return std::string("");
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
std::string text;
char *buffer = new char[size+1];
buffer[size] = 0;
if (fread(buffer, 1, size, file) == (unsigned long)size)
text = buffer;
fclose(file);
delete[] buffer;
return text;
}
static int
parse_value(const std::string &input)
{
Json::Value root;
const Json::Features features;
Json::Reader reader(features);
if (!reader.parse(input, root))
{
std::cout << "Failed to parse" << std::endl;
return 1;
}
std::cout << "ip:" << root["ip"].asString() << std::endl;
std::cout << "post:" << root["port"].asString() << std::endl;
std::cout << "operDir:" << root["operDir"].asString() << std::endl;
Json::Value interface = root["url"];
Json::FastWriter fastWriter;
for (unsigned int i = 0; i < interface.size(); i++)
{
std::cout << "###############" << std::endl;
std::cout << "name:" << interface[i]["name"].asString() << std::endl;
std::cout << "method:" << interface[i]["method"].asString() << std::endl;
Json::Value post_data = interface[i]["post_data"];
std::cout << "post_data" << post_data << std::endl;
std::string output = fastWriter.write(post_data);
std::cout << "output" << output << std::endl;
}
return 0;
}
int main( int argc, const char *argv[] )
{
int ret = 0;
std::string input = read_file("test1.json");
if (input.empty())
{
printf("Failed to read input or empty\n");
return -1;
}
ret = parse_value(input);
return ret;
}