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


```cpp
#pragma warning(disable:4996)
#include "json.h"
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 <

你可能感兴趣的:(解码json并写入文件(需加json.h头文件))