0

I'm using JSONCPP to create a JSON document with parsed latitude and longitude but it always gives me segmentation fault. I already tried to create a C++ vector such as std::vector<std::string> and after that include in JSON but the error persists.

I think it is important to say that the vectors are well constructed and if I cout them they show the values as it should. Also, all of them have the same size.

Here is this part of the code:

#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include "json/json.h"


void create_json(std::vector<std::string> vector_position_latitude,
                 std::vector<std::string> vector_degrees_latitude,
                 std::vector<std::string> vector_minutes_latitude,
                 std::vector<std::string> vector_seconds_latitude,
                 std::vector<std::string> vector_position_longitude,
                 std::vector<std::string> vector_degrees_longitude,
                 std::vector<std::string> vector_minutes_longitude,
                 std::vector<std::string> vector_seconds_longitude)
{
    // JSON STRUCTURE
    Json::Value event;
    Json::Value vecLatPL(Json::arrayValue);
    Json::Value vecLatDL(Json::arrayValue);
    Json::Value vecLatML(Json::arrayValue);
    Json::Value vecLatSL(Json::arrayValue);
    Json::Value vecLngPN(Json::arrayValue);
    Json::Value vecLngDN(Json::arrayValue);
    Json::Value vecLngMN(Json::arrayValue);
    Json::Value vecLngSN(Json::arrayValue);
    

    for (size_t i = 0; i < vector_position_latitude.size(); i++)
    {
        vecLatPL.append(vector_position_latitude[i]);
        vecLatDL.append(vector_degrees_latitude[i]);
        vecLatML.append(vector_minutes_latitude[i]);
        vecLatSL.append(vector_seconds_latitude[i]);
        vecLngPN.append(vector_position_longitude[i]);
        vecLngDN.append(vector_degrees_longitude[i]);
        vecLngMN.append(vector_minutes_longitude[i]);
        vecLngSN.append(vector_seconds_longitude[i]);

        //PREVIOUS ATTEMPT
        //vecLatPL.append(Json::Value(vector_position_latitude[i]));
        //vecLatDL.append(Json::Value(vector_degrees_latitude[i]));
        //vecLatML.append(Json::Value(vector_minutes_latitude[i]));
        //vecLatSL.append(Json::Value(vector_seconds_latitude[i]));
        //vecLngPN.append(Json::Value(vector_position_longitude[i]));
        //vecLngDN.append(Json::Value(vector_degrees_longitude[i]));
        //vecLngMN.append(Json::Value(vector_minutes_longitude[i]));
        //vecLngSN.append(Json::Value(vector_seconds_longitude[i]));
    }
    
    event["coords"]["lat"]["PL"].append(vecLatPL);
    event["coords"]["lat"]["DL"].append(vecLatDL);
    event["coords"]["lat"]["ML"].append(vecLatML);
    event["coords"]["lat"]["SL"].append(vecLatSL);
    event["coords"]["lng"]["PN"].append(vecLngPN);
    event["coords"]["lng"]["DN"].append(vecLngDN);
    event["coords"]["lng"]["MN"].append(vecLngMN);
    event["coords"]["lng"]["SN"].append(vecLngSN);

    // PREVIOUS ATTEMPTS
    // 1.
    //event["coords"]["lat"]["PL"] = vecLatPL;
    //event["coords"]["lat"]["DL"] = vecLatDL;
    //event["coords"]["lat"]["ML"] = vecLatML;
    //event["coords"]["lat"]["SL"] = vecLatSL;
    //event["coords"]["lng"]["PN"] = vecLngPN;
    //event["coords"]["lng"]["DN"] = vecLngDN;
    //event["coords"]["lng"]["MN"] = vecLngMN;
    //event["coords"]["lng"]["SN"] = vecLngSN;
   // 2.
    //event["coords"]["lat"]["PL"] = vector_position_latitude;
    //event["coords"]["lat"]["DL"] = vector_degrees_latitude;
    //event["coords"]["lat"]["ML"] = vector_minutes_latitude;
    //event["coords"]["lat"]["SL"] = vector_seconds_latitude;
    //event["coords"]["lng"]["PN"] = vector_position_longitude;
    //event["coords"]["lng"]["DN"] = vector_degrees_longitude;
    //event["coords"]["lng"]["MN"] = vector_minutes_longitude;
    //event["coords"]["lng"]["SN"] = vector_seconds_longitude;

    std::cout << event << std::endl;
};

The JSON strcuture shoud be something like:

coods:{
   lat:{
      PL:{*values*},
      DL:{*values*},
      ML:{*values*},
      SL:{*values*}
   },
   lng: {
      PN: {*values*},
      DN: {*values*},
      MN: {*values*},
      SN: {*values*}
   }
}

Can anyone help me on how to insert those vectors in the JSON document in that structure?

shizde
  • 11
  • 5
  • 2
    What does dropping this into a debugger reveal? – tadman Oct 29 '20 at 20:15
  • 2
    Either use a debugger, or just start with something simpler. Try and create the simplest possible document you can imagine, and build up slowly, instead of trying to do everything at once. – john Oct 29 '20 at 20:25
  • 1
    That `for` loop relies on all of those `vector`'s you are accessing to have at least `vector_position_latitude.size()` elements. There are no checks in your code for this. In addition to that, why not use a vector of a `struct` that describes the values, instead of 8 different vectors? – PaulMcKenzie Oct 29 '20 at 20:25

0 Answers0