2016-05-23 70 views
0

我想用JsonCpp库解析Json文件。但我面临着一个我无法解决的问题。当我解析一个文件时,下面显示的代码正常工作,但是当我添加迭代通过程序崩溃目录中的文件的部分时。解析一组Json文件一个接一个

第一个函数用于在某个目录中搜索Json文件并将其名称保存在字符串(结果)的向量中。

在主函数中,程序首先定义所需的扩展名(.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; 
    //} 


} 

回答

0

如果无法访问JSON库我帮不了你太多,但对于潜在崩溃的第一个明显的地方是if (fname.find(extension, (fname.length() - extension.length())) != string::npos)。在进行该呼叫之前,您需要确保您的文件名比分机的大小更长。

此外,对于非常深的目录树,您应该对递归进行限制,并且我知道的所有操作系统都对目录和文件名具有某种字符限制。

+0

谢谢你是对的,当我改变获取文件名的方式开始正常工作。但现在我怎样才能迭代目录中的一组文件? – Mohamed