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

getting error terminate called after throwing an instance of 'std::domain_error' what(): cannot use at() with string #1582

Closed
Munodiwa opened this issue Apr 24, 2019 · 6 comments

Comments

@Munodiwa
Copy link

After performing a http get request I receive a json string containing a string. I need to access the link inside this json string. I am using ubuntu. I downloaded the package with the cmd line sudo apt-get install nlohmann-json-dev

#include <stdio.h>
#include <stdlib.h>
#include "json.hpp"
#include <curl/curl.h>
#include

using nlohmann::json;

int main()
{
curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle;
CURLcode result; // We’ll store the result of CURL’s webpage retrieval, for simple error checking.
myHandle = curl_easy_init ( ) ;
// Notice the lack of major error checking, for brevity
curl_easy_setopt(myHandle, CURLOPT_URL, "https://matiusulung.com/se.json");
result = curl_easy_perform( myHandle);
json object = "(result)";
std::cout<<object.at("link").getstd::string();
curl_easy_cleanup( myHandle );
printf("LibCurl rules!\n");
return 0;

}

@nlohmann
Copy link
Owner

What’s the JSON you pass to the server?

@Munodiwa
Copy link
Author

Munodiwa commented Apr 24, 2019

this is the json I receive from the server {"link": "ec2-3-121-209-122.eu-central-1.compute.amazonaws.com"} which is the one I am storing in json object

@nlohmann
Copy link
Owner

Which version of the library are you using?

@nickaein
Copy link
Contributor

nickaein commented Apr 26, 2019

This line

json object = "(result)";

creates an JSON object with value type of string. Shouldn't it be

json object = json::parse(result);

instead?

@nlohmann
Copy link
Owner

@nickaein is right - you created a string with json object = "(result)";, and the error message makes sense: cannot use at() with string.

@stale
Copy link

stale bot commented May 26, 2019

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.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 26, 2019
@nlohmann nlohmann closed this as completed Jun 1, 2019
@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 21, 2019
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

3 participants