Skip to content

pass find returned iterator to numeric variable. #2031

Closed
@Alphadan28

Description

I been looking for long time already but the problem is that i don't even understand whats goin on with this iterator object which is very confusing for me.

how am i supposed to retrieve the "*it_two" value to a double variable??

in the case the value is a string is there a more direct way to get it to a char* than this:

auto keytofind = jdata.find(keyName);
std::string tmpvalue = *keytofind;
strncpy(value,tmpvalue.c_str(),MaxLen);

in this example:

 #include <iostream>
 #include <nlohmann/json.hpp>
  
 using json = nlohmann::json;
  
 int main()
 {
     // create a JSON object
     json j_object = {{"one", 1}, {"two", 2}};
  
     // call find
     auto it_two = j_object.find("two");
     auto it_three = j_object.find("three");
  
     // print values
     std::cout << std::boolalpha;
     std::cout << "\"two\" was found: " << (it_two != j_object.end()) << '\n';
     std::cout << "value at key \"two\": " << *it_two << '\n';
     std::cout << "\"three\" was found: " << (it_three != j_object.end()) << '\n';

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions