Skip to content

Finding a value in an array #399

@pboettch

Description

@pboettch

I started using nlohmann::json today (thanks for it) and I wanted to check whether a certain value is contained in an array: I used find(). It compiled but it does not work.

#include <json.hpp>
using json = nlohmann::json;
int main(void)
{
	json j({"hello", "world"});
	std::cerr << j << "\n";
	std::cerr << (j.find("hello") != j.end()) << " expecting 1\n";
	for (const auto &i: j)
		if (i == "hello")
			std::cerr << "found it while iterating\n";
    return 0;
}

it gives

["hello","world"]
0 expecting 1
found it while iterating

Reading json.hpp I saw that find() is often done with a key. What is the expected behavior of finding on an array?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions