I am reading a JSON file containing Family details and I am trying to fill them in a std::map. somehow the keys are accepting data but the values are showing null. please help.

code: test.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
#include <conio.h>

#include "nlohmann\json.hpp"
#include "extmodule.h"

using namespace std;
using json = nlohmann::json;

FAMILY tempFamily;
CHILD tempChild;

int main()
{
    std::ifstream f("sample.json");
    json data = json::parse(f);

    // Total nof items in 'family' is the no of records
    int noOfRecords = data["family"].size();


    for (int fIndex = 0; fIndex < noOfRecords; fIndex++)
    {
        int fId = 0;
        // read the family id from json data object and use the ID as main map key
        data["family"][fIndex]["fId"].get_to(fId);

        // fill the Parent node First        
        data["family"][fIndex]["fName"].get_to(tempFamily.parent.fatherName);
        data["family"][fIndex]["fAge"].get_to(tempFamily.parent.fatherAge);
        data["family"][fIndex]["mName"].get_to(tempFamily.parent.motherName);
        data["family"][fIndex]["mAge"].get_to(tempFamily.parent.motherAge);     

        // fill the child Node 
        int noOfChildren = data["family"][fIndex]["children"].size();
        rec[fId].NoOfChildren = noOfChildren;

        // cout << noOfChildren <<endl;
        for (int cIndex = 0; cIndex < noOfChildren; cIndex++)
        {
            int cId = 0;
            data["family"][fIndex]["children"][cIndex]["cId"].get_to(cId);

            data["family"][fIndex]["children"][cIndex]["cName"].get_to(tempChild.childName);
            data["family"][fIndex]["children"][cIndex]["cAge"].get_to(tempChild.childAge);

            tempFamily.child.insert(make_pair(cId, tempChild));
        }
        rec.insert(make_pair(fId, tempFamily));     
    }   
    map<int, FAMILY>::iterator fIt = rec.begin();

     while (fIt != rec.end())
    {
        FAMILY temp;
        std::cout << "Family ID: " << fIt->first <<endl;
        temp= fIt->second;
        std::cout << "Father Name: " << temp.parent.fatherName <<"\t" << fIt->second.parent.fatherAge <<endl;
        std::cout << "Mother Name: " << fIt->second.parent.motherName <<"\t" << fIt->second.parent.motherAge <<endl;
        std::cout << "\tNo of Children: "<< fIt->second.NoOfChildren<<endl;
        std::cout << "\t--------------\n\t";

        map <int, CHILD>::iterator cIt = fIt->second.child.begin();
        while (cIt != fIt->second.child.end())
        {
            std::cout <<"Child ID: "<< cIt->first;
            std::cout <<"\tName : "<< cIt->second.childName <<"\t"<<cIt->second.childAge <<endl;
            ++cIt;

        }
        std::cout <<"--------------------------------------------\n";
        ++fIt;
    }     

    _getch();
    return 0;
}
code: extmodule.h
#include <iostream>
#include <string>
#include <map>

using namespace std;

// PARENT Node Structure.

typedef struct
{
    string fatherName;
    int fatherAge;
    string motherName;
    int motherAge;
}PARENT;

//Child Node
typedef struct
{
    string childName;
    int childAge;
}CHILD;

// FAMILY Structure
typedef struct
{
    PARENT parent;
    int NoOfChildren;
    map <int, CHILD> child;
}FAMILY;

//Data Record Map

map <int, FAMILY> rec;

tempFamily Structure is getting filled but when using make_pair() to insert an element into the map it is not assigned.

Your code is very well structured and logical. One thing I saw that could potentially cause an issue is if the 'tempChild' and 'tempFamily' structures are not being reset properly for each new iteration of the loop. The following should do the trick -

int noOfChildren = data["family"][fIndex]["children"].size();
rec[fId].NoOfChildren = noOfChildren;

for (int cIndex = 0; cIndex < noOfChildren; cIndex++)
{
    int cId = 0;
    data["family"][fIndex]["children"][cIndex]["cId"].get_to(cId);

    CHILD tempChild; //Do a reset for each child...
    data["family"][fIndex]["children"][cIndex]["cName"].get_to(tempChild.childName);
    data["family"][fIndex]["children"][cIndex]["cAge"].get_to(tempChild.childAge);

    tempFamily.child.insert(make_pair(cId, tempChild));
}
rec.insert(make_pair(fId, tempFamily));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.