Json::Value 可以表示所有类型
Json::Reader 将json文件流或字符串解析到Json::Value,主要调用函数parse()
Json::Writer 将Json::Value转化为字符串流
若json文件是utf8格式,因为utf8用BOM表明编码格式,又叫utf-8的签名,意思是告诉编译器当前文件采用何种编码格式,但是BOM会产生输出,若接受者收到以EF BB BF开头的字节流,就是以utf-8格式编码的,在读取json文件时候需要去掉BOM
使用jsoncpp前提:包含jsoncpp的静态链接库,链接的时候需要连接库 -ljsoncpp,头文件包含需要用相对路径
绝对路径就是文件的真正存在的路径,是指从硬盘的根目录(盘符)开始,进行一级级目录指向文件
相对路径就是以当前文件为基准进行一级级目录指向被引用的资源文件
JSON文件:
{
"scene_id" : 40012,
"name" : "战盟广场",
"relive" : {
"type" : "1100",//复活类型
"relive_item" : 200000016,//复活使用的道具
"wait" : 2, //复活等待时间
"multi_point" : [[5,40]],//多个复活点随机选择
"state" : [100] //随机点复活
},
"layout":[{
"born_type" : "center",
"monster_sort" : 507100601, // 战盟广场普通小偷
"patrol_path" : [[29.85,26.9],[31.1,36.2],[36.9,33.25],[27.65,32.3],[34.7,26.9],[37.45,30.6],[33.8,36.2],[29.9,26.9]]//小怪行走路径
},
{
"born_type" : "center",
"monster_sort" : 507100606, // 战盟广场精英小偷
"monster_name" :1,
"monster_tree" : "bev1002.json",// 行为树
"space" : 1, // BOSS所在的分线
"is_rand_center":1, //从 center_coordxy 中随机取出两个连续的点为BOSS的出生坐标,存在即可
"select_c_radius":3,//视野范围,默认8
"move_interval":2,//巡逻周期,默认4-7秒
"rand_move_radius":3,//巡逻范围,默认3
"idle_back_distance":30,//追击范围,默认18
"born_count" : 1,
"born_range" : 0,
"center_coordxy" : [12.1,21.8,29.55,9.1,51.95,23.9,44.55,50.2,22.8,51.95],//B精英小偷出生点
"skill_set" : [409000001]
}
],
"all_reward":
{
//所有参与的玩家获得的奖励
"item_reward":[[200000058, 4, 2],[200000060, 3, 2]]
}
}
/*
* load_json.cpp
*
* Created on: 2017年9月14日
* Author: guojing
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const unsigned char *skipBOM(const unsigned char *str, int *size)
{
const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
const unsigned char *c = (const unsigned char *)str;
int i = 0;
do {
if (i > *size)
break;
if (*c != *(const unsigned char *)p++)
{
*size -= i;
return c;
}
++i;
++c;
} while (*p != '\0');
*size -= i;
return c;
}
int load_json_config(const char *doc,::Json::Value &conf)
{
int file=::open(doc,O_RDONLY);
if(file<0)
return -1;
struct stat statbuf;
if(::fstat(file,&statbuf)<0)
return -1;
void *src=0;
if((src=::mmap(0,statbuf.st_size,PROT_READ,MAP_PRIVATE,file,0))==MAP_FAILED)
{
return -1;
}
int skip_src_size = statbuf.st_size;
const unsigned char *skip_src = skipBOM((const unsigned char *)src, &skip_src_size);//去掉utf8的BOM编码标签
::Json::Reader reader;
if(reader.parse((const char*)skip_src,(const char*)(skip_src+skip_src_size),conf)==false)
{
return -1;
}
if (::munmap(src, statbuf.st_size) < 0)
{
return -1;
}
::close(file);
return 0;
}
int main()
{
//读取json文件
::Json::Value conf;
load_json_config("s40012.json",conf);
if(!conf.empty())
{
cout<
Json读取中的一些坑:可以通过getMemberNames()得到json文件中的所有外层key
const Json::Value& all_cfg = CONFIG_INSTANCE->mythical();
for(Json::Value::Members::iterator ite=all_cfg.getMemberNames().begin();ite!=all_cfg.getMemberNames().end();ite++)
{
MSG_USER("%s",(*ite).c_str());
Int64 monster_id=::atoi(ite->c_str());}
上面这样写会有一个偶发性读取不到正确key的BUG存在
正确写法:
const Json::Value& all_cfg = CONFIG_INSTANCE->mythical();
Json::Value::Members members=all_cfg.getMemberNames();
for(Json::Value::Members::iterator ite=members.begin();ite!=members.end();ite++)
{
MSG_USER("%s",(*ite).c_str());
Int64 monster_id=::atoi(ite->c_str());
}
直接迭代json文件中所有键值对,通过迭代器.key().asCString()获得对应的键,iter直接就是json文件中key所对应的完整的值
void GameConfig::BasicConfig::convert_json_to_map(int debug_flag)
{
this->revert_map().unbind_all();
for (Json::Value::iterator iter = this->revert_json().begin();
iter != this->revert_json().end(); ++iter)
{
Json::Value *json = &(*iter);
this->revert_map().rebind(::atoi(iter.key().asCString()), json);
JUDGE_CONTINUE(debug_flag == true);
MSG_DEBUG("Key: %s,%s", iter.key().asCString(),json->toStyledString().c_str());
}
}