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

Creating array of objects #1223

Closed
aruninnova opened this issue Sep 2, 2018 · 6 comments
Closed

Creating array of objects #1223

aruninnova opened this issue Sep 2, 2018 · 6 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@aruninnova
Copy link

I need to create the following structure but I am struggling to do this using json.

{
	"ParentTree": [{
		"Name": "Name3",
		"children": [{
			"Name": "Name2",
			"children": [{
				"Name": "Name1",
				"children": [{
					"Name": "Name0"
				}]
			}]
		}]
	}]
}

I have tried below but unable to get how to add name and children keys dynamically.

json jsonObj;
jsonObj["ParentTree"] = json::array();
for (auto index = 3; index >= 0; index--) {
	jsonObj["ParentTree"].push_back(json::object());
}
@aruninnova
Copy link
Author

Previously it was done using below way without using json:

std::string PResult = "\"ParentTree\":[{";
	for (int j = 3; j >= 0; j--)
	{
		std::string num = std::to_string(j);
		PResult += "\"Name\":\"";
		PResult += "Name";
		PResult +=  num + "\",";
		if (j == 0) break;
		PResult += "\"children\":[{";
	}

	PResult.erase(PResult.length() - 1);

	for (int j = 3; j >= 0; j--)
	{
		PResult += "}]";
	}

@nlohmann
Copy link
Owner

nlohmann commented Sep 2, 2018

You could use JSON Pointers. They allow to define a value with a string like /ParentTree/0/children/0/children/0/Name etc.

@aruninnova
Copy link
Author

Can you provide an example

@nlohmann
Copy link
Owner

nlohmann commented Sep 8, 2018

A brief introduction is given in the README. Here is an example.

@nlohmann
Copy link
Owner

@aruninnova Do you need further assistance?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Sep 29, 2018
@nlohmann
Copy link
Owner

💤 I closed this issue due to inactivity. Please feel free to add a comment and I shall reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants