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

How does from_json work? #662

Closed
itviewer opened this issue Jul 23, 2017 · 3 comments
Closed

How does from_json work? #662

itviewer opened this issue Jul 23, 2017 · 3 comments

Comments

@itviewer
Copy link

itviewer commented Jul 23, 2017

Sorry, I don't know if the question is right here, but I really needs help.
I need to convert json str to QString, and I defines the from_json function,

void from_json(const json& j, QString& str)
{
    str = QString::fromStdString(j.get<std::string>());
}

json object = {{"string","string"}};

then I code this ,
QString str = object["string"];
it works no problem.but when I code this,

QString str;
str = object["string"];

then the compiler do not know how to compile,error with
Error: C2593: "operator =" is not clear

Do non-built-in variables can not be pre-defined? (I tried int and double type,it can work,)

@nlohmann
Copy link
Owner

I think @theodelrieu could help here.

@itviewer
Copy link
Author

itviewer commented Jul 24, 2017

now I use

QString str;
str = object["string"].get<QString>();

to explicit type conversion, it can work. maybe
QString str = object["string"];
can do some implicit type conversion during compilation ? while I don‘’t understand why

QString str;
str = object["string"];

can't do that.

Thanks, I think this issue can be closed.

@nlohmann
Copy link
Owner

object["string"] returns a basic_json object which can implicitly convert to std::string. object["string"].get<QString>() explicitly converts to QString. I'm not sure whether QString implicitly converts from std::string though...

Happy to hear that your issue is solved. :-)

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

2 participants