逐个解析一组Json文件

时间:2016-05-23 14:05:09

标签: c++ json multiple-files

我正在尝试使用JsonCpp库解析Json文件。但是我遇到了一个我无法解决的问题。当我解析一个文件时,下面显示的代码工作正常,但是当我添加迭代文件目录中的部分时程序崩溃。

第一个函数用于在某个目录中搜索Json文件,并将其名称保存在string(results)的向量中。

在main函数中,程序首先定义所需的扩展名(.json)然后调用搜索函数。之后我尝试打开每个文件来解析它。

最后,谢谢,我真的很感激任何帮助。

#include "jsoncpp.cpp"
#include <stdio.h>
#include "json.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <dirent.h>
#include <vector>

using namespace std;



vector<string> results;             // holds search results

// recursive search algorithm
void search(string curr_directory, string extension){

    DIR* dir_point = opendir(curr_directory.c_str());
    dirent* entry = readdir(dir_point);
    while (entry){                                  // if !entry then end of directory
        if (entry->d_type == DT_DIR){               // if entry is a directory
            string fname = entry->d_name;
            if (fname != "." && fname != "..")
                search(entry->d_name, extension);   // search through it
        }
        else if (entry->d_type == DT_REG){      // if entry is a regular file
            string fname = entry->d_name;   // filename
                                                // if filename's last characters are extension
            if (fname.find(extension, (fname.length() - extension.length())) != string::npos)
                results.push_back(fname);       // add filename to results vector
        }
        entry = readdir(dir_point);
    }
    return;
}

//
//
//
//


int main(int argc, char *argv[])
{

// read Files list

    string extension; // type of file to search for
    extension = "json";

    // setup search parameters
    string curr_directory = "/Users/ITSGC_Ready2Go/3dMap";

    search(curr_directory, extension);




// loop over files
    //if (results.size()){
    //std::cout << results.size() << " files were found:" << std::endl;
    for (unsigned int z = 0; z < results.size(); ++z){  // used unsigned to appease compiler warnings

// Opening the file using ifstream function from fstream library  
    cout <<results[z].c_str()<<endl;
    Json::Value obj;
    Json::Reader reader;

    ifstream test(results[z].c_str());
    //test.open (results[z].c_str(), std::fstream::in );


// Selection objects inside the file

    reader.parse(test,obj);  

   //test >> obj;

// Parsing ID object and returning its value as integer    
   // cout << "id :" << stoi(obj["id"].asString()) <<endl;

// Parsing Line object with its internal objects

    const Json::Value& lines = obj["lines"];

    for (int i=0; i<lines.size();i++){

    cout << "index : " << i << endl;
    cout << "id:" << lines[i]["id"].asString() <<endl;
    cout << "type:" << lines[i]["type"].asString() <<endl;
    cout << "function:" << lines[i]["function"].asString() <<endl;
    cout << "color:" << lines[i]["color"].asString() <<endl;

    const   Json::Value&  poly = lines[i]["polyPoints"];

    for (int j=0; j<poly.size();j++){

    cout << "batch#"<<j<<endl;
    cout << "latitude" << poly[j]["latitude"].asFloat()<<endl;
    cout << "longitude" << poly[j]["longitude"].asFloat()<<endl;
    cout << "altitude" << poly[j]["altitude"].asFloat()<<endl;  

    }

    } 



// Reading the OccupancyGrid object 

// OccupancyGrid object is copied into constant to parse the arrays inside

   const Json::Value& occupancyGrid = obj["occupancyGrid"];
   cout << occupancyGrid.size() <<endl;

// The size of occupancyGrid is the used as number of iterations (#of rows) 

   for (int l=0; l<occupancyGrid.size();l++){

// Arrays inside occupancyGrid are copied into constant to parse the elements inside each array

    const Json::Value& element = occupancyGrid[l];

// iterations over the size of the array in order to parse every element

        cout << "row" << l << "--> ";
        for (int k=0;k<element.size();k++){

            cout << element[k].asFloat(); 
            if(k<element.size()-1){ cout<< ",";}

            }   
        cout << endl;
    }

// Parsing roadSigns object as found in the file 
// Need to understand the difference between format in the mail and the 1456 file

    const Json::Value& roadsigns = obj["roadSigns"];

    cout << "ArrayType: " << roadsigns["_ArrayType_"].asString()<<endl;

    const Json::Value& ArraySize = roadsigns["_ArraySize_"];

    for(int t=0;t<ArraySize.size();t++){

    cout << ArraySize[t].asInt(); 
    if (t<ArraySize.size()-1){ cout << " , ";}

    }

    cout<< endl;

    if (roadsigns["_ArrayData_"].asString().empty()) { 
    cout << "ArrayData: "<<roadsigns["_ArrayData_"].asFloat(); }

    else { cout << "ArrayData: empty "; }
    cout <<endl;

    test.close();
    test.clear();
    cout << "Done" << endl;
    cout << "...." << endl;
    cout << "...." << endl;

    }   
    //else{
    //  std::cout << "No files ending in '" << extension << "' were found." << std::endl;
    //}


}

1 个答案:

答案 0 :(得分:0)

如果无法访问JSON库,我无法帮助您,但潜在崩溃的第一个显而易见的地方是if (fname.find(extension, (fname.length() - extension.length())) != string::npos)。在拨打电话之前,您需要确保您的文件名长于分机的大小。

另外,对于极深的目录树,你应该限制递归,我所知道的所有操作系统都对目录和文件名有一些字符限制。