解码json并写入文件(需加json.h头文件)


```cpp
#pragma warning(disable:4996)
#include "json.h"
#include <iostream>
#include<vector>
#include <direct.h>
#include <io.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <iomanip>
using namespace std;
inline void encodeJson(string& strRet)
{
	Json::Value v;
	Json::FastWriter writer;

	v["_id"] = "ObjectId('5d738291de4cdb03132ecfed')";
	v["i_time"] = "ISODate('2019-09-07T10:12:29Z')";
	v["i_dname"] = "test";
	v["i_smac"] = "00:50:04:93:70:67";
	v["i_dmac"] = "00:02:b3:ce:70:51";
	v["i_cip"] = "10.0.0.8";
	v["i_sip"] = "10.0.0.3";
	v["i_sport"] = "NumberLong(20000)";
	v["i_is_c2s"] = "NumberLong(1)";
	v["i_saddr"] = "NumberLong(3)";
	v["i_daddr"] = "NumberLong(4)";
	v["i_func"] = "NumberLong(2)";
	v["i_groupnum"] = " NumberLong(50)";
	v["i_varnum"] = " NumberLong(1)";
	strRet = writer.write(v);
}

void decodeJsonandwrite(const string str)
{
	Json::Reader reader;
	Json::Value root;
	string s,time,dname,smac,dmac,cip,sip,sport,is_c2s,saddr,daddr,func,groupnum,varnum;
	string filename;
	if (reader.parse(str, root))
	{
		if (!root["_id"].empty())
		{
			s= root["_id"].asString();
			cout << s << endl;
		}
		int n1 = s.find("(",0);
		int n2 = s.find(")", n1);
		cout << n2 << endl;
		s = s.substr(n1 + 2, n2-n1-3);
		cout << s << endl;
		if(s == "5d738291de4cdb03132ecfed")
		{
			filename = "dnp3";
			if (!root["i_time"].empty())
			{
				time= root["i_time"].asString();
				cout << time << endl;
			}
			if (!root["i_dname"].empty())
			{
				dname = root["i_dname"].asString();
				cout << dname << endl;
			}
			if (!root["i_smac"].empty())
			{
				smac = root["i_smac"].asString();
				cout << smac << endl;
			}
			if (!root["i_dmac"].empty())
			{
				dmac = root["i_dmac"].asString();
			}
			if (!root["i_cip"].empty())
			{
				cip = root["i_cip"].asString();
			}
			if (!root["i_sip"].empty())
			{
				sip = root["i_sip"].asString();
			}
			if (!root["i_sport"].empty())
			{
				sport = root["i_sport"].asString();
				int n3 = sport.find("(", 0);
				int n4 = sport.find(")", n3);
				sport = sport.substr(n3 + 1, n4 - n3 - 1);
				cout << sport << endl;
			}
			if (!root["i_is_c2s"].empty())
			{
				is_c2s = root["i_is_c2s"].asString();
				int n5 = is_c2s.find("(", 0);
				int n6 = is_c2s.find(")", n5);
				is_c2s= is_c2s.substr(n5+ 1, n6 - n5 - 1);
				cout << is_c2s<< endl;
			}
			if (!root["i_saddr"].empty())
			{
				saddr = root["i_saddr"].asString();
				int n7= saddr.find("(", 0);
				int n8 = saddr.find(")", n7);
				saddr= "0x"+saddr.substr(n7 + 1, n8 - n7 - 1);
				cout <<saddr << endl;
			}
			if (!root["i_daddr"].empty())
			{
				daddr = root["i_daddr"].asString();
				int n9 = daddr.find("(", 0);
				int n10 = daddr.find(")", n9);
				daddr = "0x" + daddr.substr(n9 + 1, n10 - n9 - 1);
				cout << daddr << endl;
			}
			if (!root["i_func"].empty())
			{
				func = root["i_func"].asString();
				int n11= func.find("(", 0);
				int n12 = func.find(")", n11);
				func = "0x" + func.substr(n11 + 1, n12 - n11- 1);
				cout << func << endl;
			}
			if (!root["i_groupnum"].empty())
			{
				groupnum = root["i_groupnum"].asString();
				int n13= groupnum.find("(", 0);
				int n14 = groupnum.find(")", n13);
				groupnum = "0x" + groupnum.substr(n13 + 1, n14 - n13 - 1);
				cout << groupnum << endl;
			}
			if (!root["i_varnum"].empty())
			{
				varnum = root["i_varnum"].asString();
				int n15 = varnum.find("(", 0);
				int n16 = varnum.find(")", n15);
				varnum = "0x" + varnum.substr(n15 + 1, n16 - n15 - 1);
				cout << varnum << endl;
			}
			//write
			ofstream out("D:/rules/dnp3/WHITE.CFG",ios::app);
			string s1 = "00:00	23:59	1"; string s2 = "00:50:04:93:70:67    1"; string s3 = "00:50:04:93:70:67    1"; string s4 = "10.0.0.9   1"; string s5 = "255.255.255.255	1"; string s6 = "10.0.0.3   1"; string s7 = "255.255.255.255	1";
			string s8 = "20000      1"; string s9 = "1              1"; string s10 = "0x3          1"; string s11 = "0xffff       1"; string s12 = "0x12          1"; string s13 = "0x3c        1"; string s14 = "0x2          1"; string s15 = "<none>	<none>";
			out << setw(s1.length()-1) << left << "00:00	23:59	"
				<< setw(s2.length()-1) << left << smac
				<< setw(s3.length()-1) << left << dmac
				<< setw(s4.length()-1) << left << cip
				<< setw(s5.length()-1) << left << "255.255.255.255	"
				<< setw(s6.length()-1) << left << sip
				<< setw(s7.length()-1) << left << "255.255.255.255	"
				<< setw(s8.length()-1) << left << sport
				<< setw(s9.length()-1) << left << is_c2s
				<< setw(s10.length()-1) << left << saddr
				<< setw(s11.length()-1) << left << daddr
				<< setw(s12.length()-1) << left << func
				<< setw(s13.length()-1) << left <<groupnum
				<< setw(s14.length()-1) << left <<varnum
				<< setw(s15.length()-1) << left << s15 << endl;
			out.close();
		}
	}
}
int main()
{
	string s;
	encodeJson(s);
	decodeJsonandwrite(s);
	system("pause");
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值