DEV Community

Mukit, Ataul
Mukit, Ataul

Posted on

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)