DEV Community

Mukit, Ataul
Mukit, Ataul

Posted on

6 1

Convert a string to json object properly

Some of us ran into problem when it came to converting a std::string or a const char* string properly to json object by nlohmann (https://github.com/nlohmann/json).
Here is how I achieved it :

#include <string>
#include <iostream>>
#include <sstream>


#include "json.hpp"
using namespace nlohmann;

json toJson(const char* jsonString){
    json jsonObj;
    std::stringstream(jsonString) >> jsonObj;

    return jsonObj;

}

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay