-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
use json construct std::string #1462
Comments
Related: #1061 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have similar problem under MSVC 2017 (clang and gcc works). I want to convert json results to std::string vector and here is what I tried: #include "json.hpp"
#include <vector>
#include <iostream>
#include <fstream>
int main()
{
nlohmann::json jConfig2 = R"(
{
"names": ["Tim", "Tom"],
"nums": [1.2, 2.3]
}
)"_json;
std::vector<float> iNums0;
std::vector<std::string> sNames0;
// Just work for float, but std::string failed
iNums0 = std::vector<float>(jConfig2.at("nums").begin(), jConfig2.at("nums").end());
for (auto i : iNums0) { std::cout << i << " "; } std::cout << std::endl;
// Work around for MSVC 2017
auto itseed = jConfig2.at("names").get<std::vector<std::string>>();
sNames0 = std::vector<std::string>(itseed.begin(), itseed.end());
// Compile ok, but run failed: "vector iterators in range are from different containers"
// sNames0 = std::vector<std::string>(jConfig2.at("names").get<std::vector<std::string>>().begin(), jConfig2.at("names").get<std::vector<std::string>>().end());
// Comiple failed: Error C2668 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string': ambiguous call to overloaded
// sNames0 = std::vector<std::string>(jConfig2.at("names").begin(), jConfig2.at("names").end());
// Compile failed: Error C2593 'operator =' is ambiguous
// sNames0 = jConfig2.at("names");
// Compile failed: Error C2440 '<function-style-cast>': cannot convert from 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>' to 'std::vector<std::string,std::allocator<_Ty>>'
// sNames0 = std::vector<std::string>(jConfig2.at("names"));
// It works, too
std::vector<std::string> _names = jConfig2.at("names");
// sNames0 = _names;
for (auto i : sNames0) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
} The format std::vector<Type>(jsontype.at("Key").begin(), jsontype.at("Key").end()); works for Type int, float but not works under std::string. I also tried
@yuzeng2017 For your problem 1462#issue-404674231. You could try: nlohmann::json j;
j["account"] = "123456";
std::string account_text = j["account"]; // OK
// std::string account_text2(j["account"]); // FAIL
std::string account_text3(j["account"].get<std::string>()); // OK
std::cout << account_text << account_text3 << std::endl; Just use |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
What is the issue you have?
“std::basic_string<char,std::char_traits,std::allocator>::basic_string”: Overload function ambiguous.
Please describe the steps to reproduce the issue. Can you provide a small but working code example?
json j;
j["account"] = "123456";
std::string account_text(j["account"]);
vs2015 compile error.
What is the expected behavior?
And what is the actual behavior instead?
Which compiler and operating system are you using? Is it a supported compiler?
Did you use a released version of the library or the version from the
develop
branch?If you experience a compilation error: can you compile and run the unit tests?
The text was updated successfully, but these errors were encountered: