storing a json object in a vector

Hi guys, I have a question. I am working with a json file for the first time, and I am trying to store the contents of it into a vector pass it to a sorting function. However, I can't seem to print the elements of the vector which leads to be believe that i'm not successful at sorting the elements in it to begin with. He is my code. if anyone can look over it and tell me what I am doing wrong it would be a big help. thanks!


#include "quicksort.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include "json.hpp"
using namespace std;
std::vector<std::string> getKeys(nlohmann::json jsonObject);

std::vector<std::string> getKeys(nlohmann::json jsonObject) {
std::vector<std::string> keys;
for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
if (itr.key() != "metadata") {
keys.push_back(itr.key());
}
}
return keys;
}

nlohmann::json getJsonObj(std::string filename) {
std::ifstream file;
nlohmann::json jsonObject;


file.open(filename);

if (file.is_open()) {
file >> jsonObject;
}
else {
std::cout << "Unable to open file";
exit(1);
}

file.close();
return jsonObject;
}


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

if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " [filename.json]" << std::endl;
exit(1);
}
nlohmann::json jsonObject;

jsonObject = getJsonObj(argv[1]);

std::vector<std::string> keys;
keys = getKeys(jsonObject);
int size = jsonObject["metadata"]["arraySize"];

//store contents of json object in a vector:
std::vector<int>data;
std::vector<int>::iterator itr;
for(std::vector<std::string>::iterator itr = keys.begin(); itr != keys.end(); ++itr) {

std::vector<int>data = jsonObject[*itr].get<std::vector<int>>();
std::cout<<itr.value();
}

int start_s=clock();
QuickSort(&data);
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
//print sorted vector

//for(std::vector<std::string>::iterator itr = keys.begin(); itr != keys.end(); ++itr) {

//std::cout << *itr << " ";

//}
}
One problem is this:
1
2
3
4
5
6
7
std::vector<int>data; // This is not filled
std::vector<int>::iterator itr;
for(std::vector<std::string>::iterator itr = keys.begin(); itr != keys.end(); ++itr) {

std::vector<int>data = jsonObject[*itr].get<std::vector<int>>(); // This 'data' is not visible outside the loop
std::cout<<itr.value();
} 
Topic archived. No new replies allowed.