原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/2019/0912/638.html
1、常州微信小程序开发拼接json
std::vector::const_iterator iter = vec_keyinfo.begin();
//for (; iter != vec_keyinfo.end(); ++iter) {
Json::FastWriter writer;
Json::Value value;
value["shortcuts"]["order"] = iter->order_;
value["shortcuts"]["description_id"] = iter->description_id_;
value["shortcuts"]["global"] = iter->global_;
value["shortcuts"]["can_edit"] = iter->can_edit_;
value["shortcuts"]["accel"]["fVirt"] = iter->accel_.fVirt;
value["shortcuts"]["accel"]["key"] = iter->accel_.key;
value["shortcuts"]["accel"]["cmd"] = iter->accel_.cmd;
std::string output_data = writer.write(value);
//}
// output_data =
/*
{
"shortcuts": {
"accel": {
"cmd": 49253,
"fVirt": 1,
"key": 117
},
"can_edit": true,
"description_id": 15027,
"global": false,
"order": 0
}
}
*/
2、解析json到数据
/*
{
"shortcuts": [
{
"order": 0,
"description_id": 15001,
"global": false,
"can_edit": true,
"accel": {
"fVirt": 100,
"key": 10,
"cmd": 10001
}
},
{
"order": 1,
"description_id": 15002,
"global": true,
"can_edit": false,
"accel": {
"fVirt": 101,
"key": 20,
"cmd": 10002
}
},
{
"order": 2,
"description_id": 15003,
"global": true,
"can_edit": true,
"accel": {
"fVirt": 103,
"key": 30,
"cmd": 10003
}
}
]
}
*/
Json::Value value;
Json::Reader reader;
std::ifstream stream;
stream.open ("C:\\Users\\canhui.wang\\Desktop\\shortcuts.json", std::ios::binary );
if(!reader.parse(stream, value)) return vec_temp;
// 常州网站开发建设针对json数组,办法1
const Json::Value shortcuts = value["shortcuts"];
for ( unsigned int index = 0; index < shortcuts.size(); ++index ) {
int order = shortcuts[index]["order"].asInt();
int description_id = shortcuts[index]["description_id"].asInt();
bool global = shortcuts[index]["global"].asBool();
bool can_edit = shortcuts[index]["can_edit"].asBool();
int fVirt = shortcuts[index]["accel"]["fVirt"].asInt();
int key = shortcuts[index]["accel"]["key"].asInt();
int cmd = shortcuts[index]["accel"]["cmd"].asInt();
std::wostringstream wss;
wss << L"value[\"shortcuts\"] order =" << order << L" description_id= " << description_id << L" global= " << global << L" can_edit =" << can_edit << L" fVirt= " << fVirt << L" key= " << key << L" cmd= " << cmd << L"\n";
::OutputDebugString(wss.str().c_str());
}
// 针对json数组,办法2
for (Json::ValueIterator iter = value["shortcuts"].begin(); iter != value["shortcuts"].end(); iter++) {
int order = (*iter)["order"].asInt();
int description_id = (*iter)["description_id"].asInt();
bool global = (*iter)["global"].asBool();
bool can_edit = (*iter)["can_edit"].asBool();
int fVirt = (*iter)["accel"]["fVirt"].asInt();
int key = (*iter)["accel"]["key"].asInt();
int cmd = (*iter)["accel"]["cmd"].asInt();
std::wostringstream wss;
wss << L"ValueIterator order =" << order << L" description_id= " << description_id << L" global= " << global << L" can_edit =" << can_edit << L" fVirt= " << fVirt << L" key= " << key << L" cmd= " << cmd << L"\n";
::OutputDebugString(wss.str().c_str());
}
上篇:上一篇:C++解析Json用JsonCpp读写Json数据
下篇:下一篇:C++ map用法总结(整理)