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

pass find returned iterator to numeric variable. #2031

Closed
Alphadan28 opened this issue Apr 9, 2020 · 1 comment
Closed

pass find returned iterator to numeric variable. #2031

Alphadan28 opened this issue Apr 9, 2020 · 1 comment

Comments

@Alphadan28
Copy link

Alphadan28 commented Apr 9, 2020

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';
@Alphadan28 Alphadan28 changed the title find returned iterator to numeric variable. pass find returned iterator to numeric variable. Apr 9, 2020
@Alphadan28
Copy link
Author

 float x =*it_two
 printf("%lf\n",x);

the problem was how i was printing in the printf i was using %d instead of %lf

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

1 participant