使用c++ 解析json, 输出一直是 subit11 is array, 不知道哪里出了问题?
json文件:
{
"ver": "1.0",
"login": {
"user": "zhangsan",
"pwd": "123456"
},
"data": [
{
"ProjName": "文件夹1-1-1-1-1",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 8,
"pid": 6
},
{
"ProjName": "文件夹1-1-1-1-2",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 7,
"pid": 6
},
{
"ProjName": "文件夹1-1-1-1",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 6,
"pid": 5
},
{
"ProjName": "文件夹1-1-1",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 5,
"pid": 2
},
{
"ProjName": "文件夹1-2-1",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 4,
"pid": 3
},
{
"ProjName": "文件夹1-2",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 3,
"pid": 1
},
{
"ProjName": "文件夹1-1",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 2,
"pid": 1
},
{
"ProjName": "文件夹1",
"FileName": "emp.cdr",
"ProjNumber": 11,
"JpgFile": "emp.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"id": 1,
"pid": -1
}
]
}
完整c++ 代码:
#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"
bool parsejsonbody(json submit);
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) {
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;
}
if (it->is_object()){
cout << "it is object " << endl;
}
if (it->is_array() || it -> is_object()){
cout << "it is array " << endl;
parsejsonbody(*it);
}
}
return 0;
}
bool parsejsonbody(json subit){
for (auto &subit11 : subit.items()){
if (subit11.value().is_string()){
cout << "subit11: " << *subit11 << endl;
string vstri = subit11.value().get<string>();
cout << "it key: " << subit11.key() << endl;
cout << "it value: " << vstri << endl;
}
if (subit11.value().is_array() || subit11.value().is_object()){
cout << "subit11 is array " << endl;
parsejsonbody(subit11);
}
}}