how to convert json value to string ..
What did you try? Was there an error message? Did you have a look at the README?
thanks for replay sir....
i need to convert the json value ....example
json jk; json jsd;
jk.pushback(jsd);
string str=jk;
want to use 'jk' value as string....
From README
You can also get a string representation of a JSON value (serialize):
// explicit conversion to string
std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
// "happy": true,
// "pi": 3.141
// }
Can I close this issue?
@nlohmann Probably not, looks like case with operator= messed up:
json fail = R"({"foo": "bar"})"_json;
// std::string barExpected;`
// barExpected = fail["foo"]; // fails
std::string barExpected = fail["foo"]; //works
@AOrazaev I cannot reproduce the error. Which compiler are you using? What is the error message?
Maybe like this?
std::string value = parser["value"].get<std::string>();
@rue-ryuzaki Yep, I've worked around this like you said.
@nlohmann I compiled example from VS2017.
For some reason, when I'm compiling from developer console on small example using cl.exe I'm not getting this error, but from VS2017 actually I'm getting next:
Severity Code Description Project File Line Suppression State Detail Description
Error (active) E0350 more than one operator "=" matches these operands: check_json check_json.cpp 13 function "std::basic_string<_Elem, _Traits, _Alloc>::operator=(std::basic_string<_Elem, _Traits, _Alloc> &&_Right) [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]"
function "std::basic_string<_Elem, _Traits, _Alloc>::operator=(std::initializer_list<_Elem> _Ilist) [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]"
operand types are: std::string = const nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer>
Code I used to reproduce:
#include "json.hpp"
#include "stdafx.h" // only if using VS
#include <iostream>
#include <string>
using nlohmann::json;
struct JsonWrapper {
std::string _foo;
public:
JsonWrapper(const json& obj)
{
_foo = obj["foo"];
}
void log() {
std::cout << "Foo: " << _foo << std::endl;
}
};
int main() {
json obj = R"({"foo": "bar"})"_json;
JsonWrapper wrap{ obj };
wrap.log();
return 0;
}
And if insead of:
JsonWrapper(const json& obj)
{
_foo = obj["foo"];
}
Use:
JsonWrapper(const json& obj)
: _foo(obj["foo"])
{ }
It will fail in developer cmd with cl.exe too. complaining about
error C2668: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string': ambiguous call to overloaded function
@AOrazaev Your issue seems not related to the original comment.
@AOrazaev For https://github.com/nlohmann/json/issues/1061#issuecomment-385115936, using
JsonWrapper(const json& obj)
: _foo(obj["foo"].get<std::string>())
{}
works though.
I mean, probably topic starter tried his example in VS and hit issue I described before.
I personally worked around everything with get<std::string>, but bug is still there.
@Mamlesh Can I close this issue?
Most helpful comment
Maybe like this?
std::string value = parser["value"].get<std::string>();