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

auto iterator returned by find to handle value depending if is string or numeric. #2032

Closed
Alphadan28 opened this issue Apr 9, 2020 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Alphadan28
Copy link

Alphadan28 commented Apr 9, 2020

I want to make a function which finds a key by name in a json object it works fine when the value is double, but if the value comes as "130.34" it fails, what i need is a way to determine its type if is coming as string then to convert it to double.

the values may come as directly double or as double but in a string ex. "130.34"

double jfind(json jdata, char* keyName){
     auto keytofind = jdata.find(keyName);

// Check if  *keytofind is string or numeric ????? how to do it???

     if(keytofind != jdata.end()){
        return *keytofind;
     }else{
        return 0;
     }
}
@Alphadan28
Copy link
Author

sorry i found this library quite confusing. removed from my project.

@nlohmann
Copy link
Owner

What exactly did you find confusing? There is a lot of documentation around, see https://nlohmann.github.io/json/

double jfind(const json& jdata, const char* keyName)
{
    const auto it = jdata.find(keyName);
    if (it != jdata.end() && it->is_number())
    {
        return *it;
    }
    else
    {
        return 0.0;
    }
}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants