C++解析json格式文本移植第三方库jsoncpp[缺源码解析]
发布日期:2021-06-29 11:09:10 浏览次数:2 分类:技术文章

本文共 3102 字,大约阅读时间需要 10 分钟。

一;’移植第三方库

下载第三方库源码网站
https://sourceforge.net/projects/jsoncpp/

编译第三方库的静态lib

https://www.cnblogs.com/arxive/p/11220854.html

注意链接时要与生成是的格式形式一致这里都是多线程的MDT链接

https://blog.csdn.net/chenxun_2010/article/details/41847131

编译的时候还注意包含include的时候采用""容易编过,

二;使用第三方库API

1、解析json文件

解析的json文件{
"name": "BeJson", "url": "http://www.bejson.com", "page": 88, "isNonProfit": true, "address": {
"C1":{
"street": "科技园路.", "IP":"123" }, "C2":{
"street": "江苏苏州.", "IP":"1234" }, "C3":{
"street": "中国.", "IP":"12345" } }, "links": [ {
"name": "Google", "url": "http://www.google.com" }, {
"name": "Baidu", "url": "http://www.baidu.com" }, {
"name": "SoSo", "url": "http://www.SoSo.com" } ]}
解析核心代码Json::Reader reader;   Json::Value root;   int nRole = 0;   string strname;   string strurl;   int ipage;   bool bisNonProfit;   vector
vc_strMemerName; // 解析json内容 if (reader.parse(is, root)) {
strname = root["name"].asString(); strurl = root["url"].asString(); ipage = root["page"].asInt(); bisNonProfit = root["isNonProfit"].asBool(); cout << "name:" << strname << endl; cout << "url:" << strurl << endl; cout << "page:" << ipage << endl; cout << "isNonProfit:" << bisNonProfit << endl; Json::Value Obj_address; bool b_isAddressObj = root.isMember("address");//遍历对象组合 if (b_isAddressObj) {
int i_size = root["address"].size(); vc_strMemerName = root["address"].getMemberNames(); for (int i = 0; i < vc_strMemerName.size(); i++) {
cout << vc_strMemerName[i] << ":" << root["address"][vc_strMemerName[i]]["street"].asString() << endl; cout << vc_strMemerName[i] << ":" << root["address"][vc_strMemerName[i]]["IP"].asString() << endl; } } if (root["links"].isArray())//遍历数组是对象的组合 {
int i_size = root["links"].size(); for (int i = 0; i < i_size; i++) {
cout << root["links"][i]["name"].asString() << endl; cout << root["links"][i]["url"].asString() << endl; } } } is.close();

2、递归解析菜单库,输出菜单层级

其格式为有部分对象节点中包含children数组节点(其中包含多个对象,而对象里面又可以包含children数组)

大致为很多children这种类型,提取出children子菜单的菜单层级表{
"data": {
"jsonMenu": {
"id":"123", "displayName":"zw", "Type":"P", "children": [ {
"id":"1233", "displayName":"z2w", "Type":"P1", "children": [ ] }, {
"id":"12335", "displayName":"z2w2", "Type":"P13", "children": [ {
"id":"12336", "displayName":"z2w42", "Type":"P13234", "children": [ ] }, {
"id":"1233ss6", "displayName":"z2w4sd2", "Type":"P13qwew234", "children": [ "id":"1s6", "displayName":"w4sd2", "Type":"P134", "children": [ ] ] } ] } ] } }}

在这里插入图片描述

三;jsonCpp源码学习(如果解析出json文件数组进行存储到root中来让我们提取的)
等C++课程第三部分看完,及时填充上,以及解析XML格式文件和其源码

转载地址:https://blog.csdn.net/zw1996/article/details/108249996 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++学习 -4 查询C++标准库写应用层代码以filestream【缺少对标准库的解析】
下一篇:Python3.7离线安装Requests无法正常使用问题(转载)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月12日 17时38分22秒