c++之json
Posted suny5
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++之json相关的知识,希望对你有一定的参考价值。
1.rapidjson 腾讯开源
githup:https://github.com/Tencent/rapidjson
使用文档:http://rapidjson.org/zh-cn/
Installation
RapidJSON is a header-only C++ library. Just copy the include/rapidjson
folder to system or project‘s include path.
解析json例子
{ "id": "dd2fb82d-26a5-48d6-abf9-6d8ea7889b3f", "imp": [{ "id": "1963", "native": { "assets": [{ "id": 0, "required": 1, "title": { "len": 50 } }, { "id": 1, "required": 1, "img": { "w": 500, "h": 300, "type": 3 } }, { "id": 2, "required": 0, "data": { "type": 2 } }] } }], "device": { "ua": "testua", "geo": { "lat": 39.99559783935547, "lon": 116.40010070800781 }, "ip": "58.24.0.17", "deviceType": 4, "make": "Xiaomi", "model": "7 Plus", "os": "android", "osv": "7.35.3", "h": 1136, "w": 640, "carrier": 1, "connectiontype": 2, "did": "862224036233396", "dpid": "bf908f5a193294f2", "mac": "02:00:00:00:00:00" } }
#include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include <iostream> using namespace rapidjson; //解析json int main() { // 1. Parse a JSON string into DOM. const char* json = "{"id":"dd2fb82d-26a5-48d6-abf9-6d8ea7889b3f","imp":[{"id":"1963","native":{"assets":[{"id":0,"required":1,"title":{"len":50}},{"id":1,"required":1,"img":{"w":500,"h":300,"type":3}},{"id":2,"required":0,"data":{"type":2}}]}}],"device":{"ua":"testua","geo":{"lat":39.99559783935547,"lon":116.40010070800781},"ip":"58.24.0.17","deviceType":4,"make":"Xiaomi","model":"7 Plus","os":"Android","osv":"7.35.3","h":1136,"w":640,"carrier":1,"connectiontype":2,"did":"862224036233396","dpid":"bf908f5a193294f2","mac":"02:00:00:00:00:00"}}"; rapidjson::Document dom; if(dom.Parse(json).HasParseError()==0){ if(dom.HasMember("id") && dom["id"].IsString()){ std::cout<<"id:"<<dom["id"].GetString()<<std::endl; } if(dom.HasMember("imp") && dom["imp"].IsArray()){ const rapidjson::Value& imp = dom["imp"]; for(int i =0;i<imp.Size();i++){ if(imp[i].HasMember("id") && imp[i]["id"].IsString()){ std::cout<<"id:"<<imp[i]["id"].GetString()<<std::endl; } if(imp[i].HasMember("native")&& imp[i]["native"].IsObject()){ const rapidjson::Value& native = imp[i]["native"]; if(native.HasMember("assets")){ const rapidjson::Value& assets = native["assets"]; for(int i =0;i<assets.Size();i++){ const rapidjson::Value& obj = assets[i]; std::cout<<"assets:id: "<<obj["id"].GetInt()<<std::endl; std::cout<<"assets:required: "<<obj["required"].GetInt()<<std::endl; if(obj.HasMember("title")){ std::cout<<"assets:title:len: "<<obj["title"]["len"].GetInt()<<std::endl; } if(obj.HasMember("img")){ std::cout<<"assets:img:w: "<<obj["img"]["w"].GetInt()<<std::endl; std::cout<<"assets:img:h: "<<obj["img"]["h"].GetInt()<<std::endl; std::cout<<"assets:img:type: "<<obj["img"]["type"].GetInt()<<std::endl; } if(obj.HasMember("data")){ std::cout<<"assets:data:type: "<<obj["data"]["type"].GetInt()<<std::endl; } } } } } } if(dom.HasMember("device")){ std::cout<<"device:ua: "<<dom["device"]["ua"].GetString()<<std::endl; std::cout<<"device:ip: "<<dom["device"]["ip"].GetString()<<std::endl; if(dom["device"].HasMember("geo")){ std::cout<<"device:geo:lat: "<<dom["device"]["geo"]["lat"].GetDouble()<<std::endl; std::cout<<"device:geo:lon: "<<dom["device"]["geo"]["lon"].GetDouble()<<std::endl; } std::cout<<"device:deviceType: "<<dom["device"]["deviceType"].GetInt()<<std::endl; std::cout<<"device:make: "<<dom["device"]["make"].GetString()<<std::endl; std::cout<<"device:model: "<<dom["device"]["model"].GetString()<<std::endl; std::cout<<"device:os: "<<dom["device"]["os"].GetString()<<std::endl; std::cout<<"device:osv: "<<dom["device"]["osv"].GetString()<<std::endl; std::cout<<"device:h: "<<dom["device"]["h"].GetInt()<<std::endl; std::cout<<"device:w: "<<dom["device"]["w"].GetInt()<<std::endl; std::cout<<"device:carrier: "<<dom["device"]["carrier"].GetInt()<<std::endl; std::cout<<"device:connectiontype: "<<dom["device"]["connectiontype"].GetInt()<<std::endl; std::cout<<"device:did: "<<dom["device"]["did"].GetString()<<std::endl; std::cout<<"device:dpid: "<<dom["device"]["dpid"].GetString()<<std::endl; std::cout<<"device:mac: "<<dom["device"]["mac"].GetString()<<std::endl; } } return 0; }
以上是关于c++之json的主要内容,如果未能解决你的问题,请参考以下文章