#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "json.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ProtocolParser.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using nlohmann::json;
#define filename "data.json"
int main(void)
{
ifstream file(filename, ios::in | ios::binary);
if(!file.is_open()){
throw runtime_error("Faile open the file.");
}
file.seekg(0,ios::end);
streamsize fileSize = file.tellg();
if(fileSize <=0){
throw runtime_error("File is emppty");
}
char *buffer = new char[fileSize];
file.seekg(0,ios::beg);
file.read(buffer, fileSize);
cout << fileSize << "filesize" << endl;
if(!file || static_cast<streamsize>(content.size()) != fileSize) {
throw runtime_error("Reading failed or did not read all data from the file.");
}
file.close();
ProtocolParser parse;
json j = parse.doParse(buffer);
for (auto it = j.begin(); it != j.end(); ++it){
if (it->is_string()){
string vstr = it.value().get<string>();
cout << "it key: " << it.key() << endl;
cout << "it value: " << vstr << endl;
}
// cout << *it << endl;
// cout << it.key() << endl;
// cout << it.value() << endl;
if (it->is_array()){
cout << "it is array " << endl;
try {
ProtocolParser parse1;
string valueStr = it.value();
const char* valueChar = valueStr.c_str();
size_t len = strlen(valueChar);
char* recvdata = new char[len];
strcpy(recvdata, valueChar);
json k = parse1.doParse(recvdata);
delete []recvdata;
for (auto subit = k.begin(); subit != k.end(); ++subit){
if (subit->is_string()){
cout << "subit: " << *subit << endl;
}
if (subit->is_array()){
string vstr1 = subit.value().get<string>();
cout << "subit: " << vstr1 << endl;
continue;
}
// cout << subit.key() << endl;
// cout << subit.value() << endl;
}
}catch(exception& e){
cerr << "err parsing JSON:" << e.what() << endl << endl;
}
}
}
return 0;
}
代码里面加入了两个for 循环,目的就是不让进入异常, 可是还是进入了异常, 是不是continue放错地方了,另外,能不能写成递归函数? 可以不断循环解析array