【HTTP服务器】JSONCPP——入门篇

系列文章目录



前言


一、JSON

1、jsoncpp

jsoncpp 是cpp对json数据格式的调用库,需要安装jsoncpp,cpp才能使用json数据格式。

jsoncpp的安装参考以下链接
https://blog.csdn.net/wesigj/article/details/116643589

2、nlohmann/json

nlohmann/json 是一个用于解析json的开源c++库,是一个header only的库,也就是说只要包含一个.hpp文件,那么你就可以使用该库。

源码:
https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp

二、使用步骤

1、json文件

代码如下:
test.json

{
	"filename" : "example.json",
	"string" : "Hello World!",
	"filesize" : 1024,
	"inttype" : 123,
	"floattype" : 3.14,
	"doubletype": 1.2345,
	"arrayint" : [1001,1002,1003,1004],
	"arraycahr": ["a", "b", "c", "d"],
	"arrayobj1" : [{"a":1, "b":2},{"c":3},{"d":4}],
	"arrayobj2" : [{"name":"张三","age":18,"weight":60.3},{"name":"李四","age":19,"weight":66.6}]
}

2、jsoncpp从json文件中读取json

#include <jsoncpp/json/json.h> 
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;


void demo_simple()
{
    ifstream ifs;
    ifs.open("test.json");
    //.cpp文件当前目录可以直接打开,否则相对路径和绝对路径也可以

    assert(ifs.is_open());
 
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(ifs, root, false))
    {
        cerr << "parse failed \n";
        return;
    }
 
    // string name = root["pathType"].asString();
    // int age = root["inttype"].asInt();
 
    // cout << "demo read from filet result =============\n";
    // cout << "pathType : " << name << endl;
    // cout << "inttype: " << age << endl;

   Json::Value array = root["arraycahr"];
   for(int i = 0;i < array.size();i++)
   {
	   std::cout<<array[i]<<endl;
	//    std::cout<<array[i].asString()<<endl;
	
   }
    // cout<<array[0]["a"]<<endl;

    ifs.close();
}

int main()
{
    demo_simple();
    return 0;
}


#include <jsoncpp/json/json.h> 

// #include <cstdio>
// #include <cstring>
// #include <string>
// #include <iostream>
  
  //json
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;
 
void demo_simple()
{
    ifstream ifs;
    ifs.open("../scripts/json_structure/mediator2ros.json");

    assert(ifs.is_open());
 
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(ifs, root, false))
    {
        cerr << "parse failed \n";
        return;
    }
 
     Json::Value name = root["arraycahr"];
    Json::Value age = root["inttype"];

    // string name = root["filename"].asString();
    // int age = root["inttype"].asInt();
 
    cout << "demo read from filet result =============\n";
    cout << "pathType : " << name << endl;
    cout << "inttype : " << age << endl;

//    Json::Value array = root["arraycahr"];
//    for(int i = 0;i < array.size();i++)
//    {
// 	   std::cout<<array[i]<<endl;
// 	//    std::cout<<array[i].asString()<<endl;
//    }
    // cout<<array[0]["a"]<<endl;

    ifs.close();
}


int main()
{
    // string test ="{\"id\":1,\"name\":\"kurama\"}";
    // Json::Reader reader;
    // Json::Value value;
    // if(reader.parse(test,value))
    // {
    //   if(!value["id"].isNull())
    //   {
    //     cout<<value["id"].asInt()<<endl;
    //     cout<<value["name"].asString()<<endl;
    //   }
    // }
    demo_simple();
    return 0;
}


3、nlohmann/json 从json文件中读取json

#include "json.hpp"
  //json
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
// using namespace std;
 
// for convenience
using json = nlohmann::json;


void demo_simple()
{
    json j;
    std::ifstream("../scripts/mediator_json/mediator2ros.json") >> j;

    // std::cout<<j<<std::endl;
    // std::cout<<    j["doubletype"]<<std::endl;

    // std::ifstream read("../scripts/json_structure/mediator2ros.json");
	// json in = json::parse(read);
    
	// json in = json::parse(std::ifstream("../scripts/mediator_json/mediator2ros.json"));
	// std::cout << in.dump(4) << std::endl;

    json _robot_state = json::parse(std::ifstream("../scripts/state_api_json/robot_state.json"));
	std::cout << _robot_state.dump(4) << std::endl;


	//一行代码从json文件读取json
	//json _relocation_manual = json::parse(std::ifstream("../scripts/ros/control_api_json/relocation_manual.json"));

}


int main()
{
    demo_simple();
    return 0;
}



总结

以上就是今天要讲的内容

本文作者:WeSiGJ

参考链接(包括但不限于):
https://github.com/nlohmann/json
https://www.cnblogs.com/maizhongfei/p/14164895.html

欢迎各位兄弟姐妹们,加入C++ HTTP服务器开发技术交流群:

QQ群:426685924

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WeSiGJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值