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

Insertion into nested json field #621

Closed
karthiksheshadri opened this issue Jun 16, 2017 · 6 comments
Closed

Insertion into nested json field #621

karthiksheshadri opened this issue Jun 16, 2017 · 6 comments

Comments

@karthiksheshadri
Copy link

karthiksheshadri commented Jun 16, 2017

Suppose one wants to insert iteratively into a nested field, for example:

for (auto& i : strings) {

j["Outer level"]["Inner level[i]"]=match[i];

}

pushback() does not do this and one cannot use a string type as index, i.e, j["Outer Level"][str] doesn't work - is there any way to do this?

Many thanks!

@nlohmann
Copy link
Owner

What would be match in your example?

@nlohmann
Copy link
Owner

This works, but may not be what you want:

#include "json.hpp"

using json = nlohmann::json;

int main()
{
    std::vector<std::string> strings = {"one", "two", "three"};
    json j;
    
    for (auto& i : strings) {
        j["Outer level"]["Inner level[i]"][i] = i;
    }
    
    std::cout << j << std::endl;
}

@karthiksheshadri
Copy link
Author

match would be a string, for example, std:string match = "Hello";.

Thanks!

@nlohmann
Copy link
Owner

nlohmann commented Jun 16, 2017

And what is strings? I need more information. Also, please post the error you experience.

@karthiksheshadri
Copy link
Author

karthiksheshadri commented Jun 16, 2017

int ii = 0;
	for (auto& i : strings) {
		if (std::regex_match(i, match, rgx)) {
			std::ssub_match submatch = match[1];
			j["includes"][ii] = submatch.str();
			std::cout << submatch.str() << '\n';
			ii++;
		}
	}

This works perfectly, thank you!

@nlohmann
Copy link
Owner

Thanks for checking back!

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

No branches or pull requests

2 participants