My libcurl code returns me json string:
{"object":"user","attributes":{"id":000,"admin":false,"username":"un","email":"[email protected]","first_name":"leopold95","last_name":"leopold95","language":"en"}}
And i want parse this string in my objects (my problem isnt in it). But when i trying parse this with nlohmann or rapid I have an error. But if I try parse this string:
string str = "{\"object\":\"user\",\"attributes\":{\"id\":36,\"admin\":false,\"username\":\"a4qsmr9m\",\"email\":\"[email protected]\",\"first_name\":\"leopold95\",\"last_name\":\"leopold95\",\"language\":\"en\"}}";
with nlohmann everything works fine. with rapid i havent errors too.
So, my question is:
How could I parse this string (1st) with nlohmann ?
or
How could I replace all " to \" in my respose?
And, ofcurse replace(s, "", ""); replace(s, '', ''); , replace(s.begin(), s.end(), ""/'', ""/''); dont work for me. in some situation i have my "replaced symbols" in the end of string
If u need I can add more code or publish project to git
EDIT:
UserInfo Engine::testIU(string link)
{
CURL* curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
struct curl_slist* headers = NULL; //headers struct
if (curl) {
//headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer imr8VuU9L6qUTjyZQ0srCNyfFE7Ru0v8VOLZBMd8bhjxJFfP");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); //add headers
curl_easy_setopt(curl, CURLOPT_URL, link.c_str()); //set url
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); //set http method
//curl_easy_setopt(curl,CURLOPT_RETURNTR, true);
//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &readBuffer); /* data goes here */
res = curl_easy_perform(curl);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
json j = json::parse(readBuffer);
testUserI.id = j["attributes"]["id"].get<int>();
testUserI.admin = j["attributes"]["admin"].get<bool>();
testUserI.username = j["attributes"]["username"].get<string>();
testUserI.email = j["attributes"]["email"].get<string>();
return testUserI;
}
struct UserInfo
{
int id = 0;
bool admin = NULL;
string username = "";
string email = "";
string first_name = "";
string last_name = "";
};
{"object":"user","attributes":{"id":0,"admin":false,"username":"a4qsmr9m","email":"[email protected]","first_name":"leopold95","last_name":"leopold95","language":"en"}}
EDIT 2: so if u have the same problem just fix this in your code:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
and see answer above and all commets there