Skip to content

How to use a json::iterator dereferenced value in code? #1120

Closed
@014972304505347

Description

I am trying to do something like this - it involves taking a json::iterator through some json objects, and getting their keys and values for entries. I want to use the values (i.e. it.value() and mit.value()) as arguments to other functions...

    json result;
    json inboundMessage = ...;//determined at runtime
    json savedMessage = ...; //determined at runtime
    for (json::iterator it = inboundMessage.begin(), mit = savedMessage.begin(); it != inboundMessage.end() && mit != savedMessage.end(); ++it, ++mit) {
	json pair;
	pair.emplace_back("log", it.value());
        pair.emplace_back("data", mit.value());
	result.emplace_back(it.key(), pair);
    }
    return result;

When I build, I get the following error:
'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::basic_json(nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &&) noexcept': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::initializer_list<nlohmann::detail::json_ref<nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>>'

What is the correct way to access the value referenced by an iterator such that the value can be used by code?

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions