irpas技术客

nlohmann json简单用法,C++使用json, json与string转换使用笔记_gdizcm_c++ json转string

网络 3519

C++解析json时,使用nlohmann json工具解析方便快捷。把它记录下来,方便以后查阅。

//地址:https://github.com/nlohmann/json, 需要引用的头文件位置:single_include/nlohmann/json.hpp //需要引用的头文件,在使用时,将这个文件加入到工程中即可,只添加这一个文件就行 //#include "json.hpp" //using nlohmann::json;

//创建普通元素和子元素

/*{ ? ? "answer":{"everything":42}, ? ? "happy":true, ? ? "list":[0,1,2], ? ? "name":"Mike", ? ? "nothing":null, ? ? "object":{ ? ? ? ? "currency":"USD", ? ? ? ? "value":23 ? ? }, ? ? "pi":3.14 }*/ // ? ? json j; ? ? j["pi"] = 3.14; ? ? j["happy"] = true; ? ? j["name"] = "Mike"; ? ? j["nothing"] = nullptr; ? ? j["answer"]["everything"] = 42; ? ? j["list"] = {0, 1, 2}; ? ? j["object"] = {{"currency", "USD"}, {"value", 23}}; ? ? cout<<j.dump()<<endl;

//创建空的对象 ? ? //{} ? ? json empty_implicit = json({}); ? ? json empty_explicit = json::object(); ? ? cout<<empty_implicit << empty_explicit<<endl;

//创建数组

//[] json empty_array = json::array(); cout<<empty_array.dump()<<endl; //[["currency","USD"],["value",23]] json jArray = json::array({{"currency", "USD"}, {"value", 23}}); cout<<jArray.dump()<<endl; //["foo",1,true] json jpushArray; jpushArray.push_back("foo"); jpushArray.push_back(1); jpushArray.push_back(true); cout<<jpushArray.dump()<<endl; //[{"CPU":"100","MEM":"123456"},{"GPU":"100","Memory":"123456"}] json jArrayJson = json::array(); json arrayElement; arrayElement["CPU"] = "100"; arrayElement["MEM"] = "123456"; jArrayJson.push_back(arrayElement); json arrayElement2; arrayElement2["GPU"] = "100"; arrayElement2["Memory"] = "123456"; jArrayJson.push_back(arrayElement2); cout<<jArrayJson<<endl; //Create from strings //create object from strings literal //{"happy":true,"pi":3.14} json createJson_1 = "{\"happy\": true, \"pi\":3.14}"_json; cout<<createJson_1.dump()<<endl; auto createJson_2 = R"( ? ? { ? ? ? ? "happy": true, ? ? ? ? "pi": 3.14 ? ? } )"_json; cout<<createJson_2.dump()<<endl; auto createJson_3 = json::parse(R"({"happy": true, "pi": 3.14})"); cout<<createJson_3.dump()<<endl; cout<<createJson_3.dump(4)<<endl; ? ? ? //这个4是缩进的大小

//异常处理(重要)

json::parse()异常处理: 1. //为了避免匹配失败导致程序异常,需要修改为 auto createJson_31 = json::parse(R"({"happy": true, "pi": 3.14})", nullptr, false);

//parse的第二个参数是回调函数指针,可以为空,第三个参数指示是否抛出异常,可以改为false,然后使用前增加判断 if (!(createJson_31.is_null()) && !(createJson_31.is_discarded())) { }

2.//用try catch捕捉异常

const string sJson="dfdsard"; try { json j2 = json::parse(sJson.c_str()); } catch (json::parse_error& ex) { std::cerr << "parse error " << ex.what() << std::endl; }

3. //提前判断字符串是否能转成json

string sJson="123123"; if (!json::accept(sJson)) { std::cerr << "parse error" << std::endl; }

?

其他异常判断 //在访问json数据前,应判断是否存在 if (createJson_31.contains("happy")) {

} //在取值前,应判断类型,类型不匹配会出现异常 if (createJson_31["happy"].is_boolean()) { ? ? bool ha = createJson_31["happy"]; ? ? cout<<ha<<endl; } if (createJson_31["pi"].is_number_float()) { ? ? double pi = createJson_31["pi"]; ? ? cout<<pi<<endl; }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #C #json转string #json工具解析方便快捷 #把它记录下来方便以后查阅