Json笔记

Json记录

C++ Json配置

sudo apt install libjsoncpp-dev

读操作

#include <jsoncpp/json/json.h>


std::string Arm_Device = "on",Plate_Device = "on",Video_Device = "on";
std::string Lidar_Device = "on",Switch_Device = "on";
std::string CAM_FRONT_IP="192.168.2.55",CAM_MIDDLE_IP="192.168.2.54",CAM_BACK_IP="192.168.2.53";
std::string Recv_IP = "172.8.3.16",Send_IP = "172.8.3.04",Send_Port="4002";
void readDataFromJson()
{
	Json::Reader reader;/*用于按照JSON数据格式进行解析*/
	Json::Value root;/*用于保存JSON类型的一段数据*/
	
	ifstream srcFile("./warning.json", ios::binary);/*定义一个ifstream流对象,与文件demo.json进行关联*/
	if (!srcFile.is_open())
	{
		printf("Fail to open src.json\n");
		return;
	}
	/*将demo.json数据解析到根节点root*/
	if (reader.parse(srcFile, root))
	{
		/*读取根节点信息*/
		Arm_Device = root["Arm"].asString();
		Plate_Device= root["Plate"].asString();
		Lidar_Device = root["Lidar"].asString();
		Video_Device = root["Video"].asString();
		Switch_Device = root["Switch"].asString();

		Recv_IP = root["Recv_IP"].asString();
		Send_IP = root["Send_IP"].asString();
		Send_Port = root["Send_Port"].asString();
		
		if(Switch_Device == "on")
		{
			CAM_FRONT_IP = root["SwitchClass"][0]["CAM_FRONT_IP"].asString();
			CAM_MIDDLE_IP = root["SwitchClass"][0]["CAM_MIDDLE_IP"].asString();
			CAM_BACK_IP = root["SwitchClass"][0]["CAM_BACK_IP"].asString();
		}
		else
		{
			CAM_FRONT_IP = root["SwitchClass"][1]["CAM_FRONT_IP"].asString();
			CAM_MIDDLE_IP = root["SwitchClass"][1]["CAM_MIDDLE_IP"].asString();
			CAM_BACK_IP = root["SwitchClass"][1]["CAM_BACK_IP"].asString();
		}
		// printf("Arm:%s\nPlate:%s\nVideo:%s\n",Arm_Device.c_str(),Plate_Device.c_str(),Video_Device.c_str());
		// printf("%s\n%s\n%s\n",Front.c_str(),Middle.c_str(),Back.c_str());
	}
	srcFile.close();
}

上述读取的warning.json文件信息(配置开关)

{
   "Arm" : "on",
   "Lidar" : "on",
   "Plate" : "on",
   "SwitchClass" : [
      {
         "CAM_BACK_IP" : "192.168.3.66",
         "CAM_FRONT_IP" : "192.168.3.64",
         "CAM_MIDDLE_IP" : "192.168.3.65"
      },
      {
         "CAM_BACK_IP" : "192.168.6.66",
         "CAM_FRONT_IP" : "192.168.4.64",
         "CAM_MIDDLE_IP" : "192.168.5.65"
      }
   ],
   "Switch" : "on",
   "Video" : "on",
   "Recv_IP" : "172.16.3.104",
   "Send_IP" : "172.16.3.102",
   "Send_Port" : "6001"
}

写操作

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

void writeDataToJson()
{
	Json::Value root;/*JSON文件的根节点*/

	/*根节点的属性*/
	root["Arm"] = Json::Value("on");
	root["Lidar"] = Json::Value("on");
	root["Plate"] = Json::Value("on");
	/*数组形式*/
	root["Combine"].append("tea");
	root["Combine"].append("coffee");
	root["Combine"].append("milk");

	Json::Value branch1,branch2;
	branch1["Switch"]=Json::Value("on");
   	branch1["Video"]=Json::Value("on");
   	branch2["Recv_IP"]=Json::Value("192.168.7.2");
   	branch2["Send_IP"]=Json::Value("192.168.7.3");
	
	/*将子节点挂在根节点上*/
	root["Config"].append(branch1);
	root["Config"].append(branch2);

	/*缩进输出到终端*/
	Json::StyledWriter sw;
	cout << sw.write(root) << endl << endl;
	/*输出到JSON文件*/
	ofstream desFile("../des.json", std::ios::out | std::ios::app);
	if (!desFile.is_open())
	{
		cout << "Fail to pen des.jons";
		return;
	}
	desFile << sw.write(root);
	desFile.close();
}

在这里插入图片描述

字符串转换Json(UTF-8编码)

std::string transformJSON(int isPark,int isObstacles,int isVehicle,int isPlate,std::string plate_number,std::string color_STR,int isOne2Port,float score)
{
	// utf-8 编码
    static Json::Value def = []() {
		Json::Value def;
		Json::StreamWriterBuilder::setDefaults(&def);
		def["emitUTF8"] = true;
		return def;
	}();

	Json::Value root;
    std::string jsonString;
    root["isPark"]=isPark; 
    root["isObstacles"]=isObstacles;
    root["isVehicle"]=isVehicle;
    root["isPlate"]=isPlate;
    root["plate_number"]=plate_number;
    root["color_STR"]=color_STR;
    root["isOne2Port"]=isOne2Port;
    root["score"]=score;

    std::ostringstream stream;
	Json::StreamWriterBuilder stream_builder;
	stream_builder.settings_ = def;//Config emitUTF8
	std::unique_ptr<Json::StreamWriter> writer(stream_builder.newStreamWriter());
	writer->write(root, &stream);
	return stream.str();
}

python版本

import json

def toJSon(isVehicle,isPark,isObstacles,isPlate,plate_number,plate_score,color_STR,isOne2Port):
    root = dict()
    root["isPark"]=isPark; 
    root["isObstacles"]=isObstacles;
    root["isVehicle"]=isVehicle;
    root["isPlate"]=isPlate;
    root["plate_number"]=plate_number;
    root["color_STR"]=color_STR;
    root["isOne2Port"]=isOne2Port;
    root["score"]=plate_score;
    final=json.dumps(root,ensure_ascii=False,indent=4)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Q_Outsider

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

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

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

打赏作者

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

抵扣说明:

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

余额充值