Skip to content

getting json array inside json object #2135

Description

@UIysse

Hi,

I've spent a few hours on this and I read full doc.
I store my data fine using this code

	std::ofstream foutSave("Etudes2.json");
	if (foutSave.is_open())
	{
		using json = nlohmann::json;
		auto jsonObjectsArray = json::array();
		for (auto const &o : _vecEtude)//iterate les etudes
		{
			auto jsonObjectsArrayCritInclusion = json::array();
			auto jsonObjectsArrayCritExclusion = json::array();
			json jsonEtudeNom;
			jsonEtudeNom["_nom etude"] = o._nom.toStdString();
			for (auto const &i : o._vecCritereInclusion)//iterate le vecteur des criteres d'inclusion
			{
				json jsonObject;
				jsonObject["critere inclusion"] = i._nom.toStdString();
				jsonObjectsArrayCritInclusion.push_back(jsonObject);
			}
			jsonEtudeNom["criteres inclusion"] = { jsonObjectsArrayCritInclusion };
			for (auto const &i : o._vecCritereExclusion)//iterate le vecteur des criteres d'exclusion
			{
				json jsonObject;
				jsonObject["critere exclusion"] = i._nom.toStdString();
				jsonObjectsArrayCritExclusion.push_back(jsonObject);
			}
			jsonEtudeNom["criteres exclusion"] = { jsonObjectsArrayCritExclusion };
			jsonObjectsArray.push_back(jsonEtudeNom);
		}
		foutSave << jsonObjectsArray.dump(4) << std::endl; //.dump(4) allows pretty formatting, see nlohmann/json for more information.
	}

I get a good file, such as this one :

[
    {
        "_nom etude": "etude 1",
        "criteres exclusion": [
            {
                "critere exclusion": "exclu 1"
            },
            {
                "critere exclusion": "exclu 2"
            }
        ],
        "criteres inclusion": [
            {
                "critere inclusion": "inclu 1"
            },
            {
                "critere inclusion": "inclu 2"
            }
        ]
    },
    {
        "_nom etude": "etude 2",
        "criteres exclusion": [
            {
                "critere exclusion": "exclu 2"
            }
        ],
        "criteres inclusion": [
            {
                "critere inclusion": "inclu 2"
            }
        ]
    }
]

I get an array of etude each of which contains a name, a json array of "critere exclusion" and a json array of "critere inclusion".

The problem is reading the file.
I get the first array which i iterate with for (auto& p : jsonObject), it runs okay untill

else if (it.key() == "criteres inclusion")
However even tho this condition succeds, i can't retrieve the array associated with this key.
I've tried
auto jsonArrayCritIncl = it.value();
and
auto jsonArrayCritIncl = it.value().get<json::array>();
nothing works, could you please help me ?

		using json = nlohmann::json;
		json jsonObject = json::parse(finLoad);
		for (auto& p : jsonObject)
		{
			std::string mystr;
			Etude uneEtude;
			for (auto it = p.begin(); it != p.end(); ++it)
			{
				if (it.key() == "_nom etude")
				{
					mystr = it.value().get<std::string>();
					uneEtude._nom = QString::fromStdString(mystr);
				}
				else if (it.key() == "criteres inclusion")
				{
					//
					auto jsonArrayCritIncl = it.value();
					for (auto const& k : jsonArrayCritIncl)
					{
						//Do stuff
					}
				}
                       }
                }
      }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions