Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting json array inside json object #2135

Closed
UIysse opened this issue May 25, 2020 · 1 comment
Closed

getting json array inside json object #2135

UIysse opened this issue May 25, 2020 · 1 comment

Comments

@UIysse
Copy link

UIysse commented May 25, 2020

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
					}
				}
                       }
                }
      }
}
@UIysse
Copy link
Author

UIysse commented May 25, 2020

I dont know what I did wrong in the first place but i tried again and it now does compile and work good. Closing this issue.

@UIysse UIysse closed this as completed May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant