I am trying to understand JSON to string and string to JSON conversion using nlohmann framework for JSON library in C++. However I am getting problem with exception generated and I am not understanding the reason why it is being generated.
The following is the code I have used from nlohmann framework. I tried to create a JSON object "j_string", add attribute "transformation_matrix" and add a corresponding value to it, of type string from a predefined variable "greetings". Now I am trying to convert from string to JSON, then parse it and again convert to string. Basically I am trying to send these data over UDP, which is whole idea behind my project. To do that, I need to convert from string to JSON, do some computation to extract a certain value of an attribute and then convert it back to string and send over UDP. When I try to convert from string to JSON, I get exception from compiler. I am using Visual Studio 2019. The following is the exception generated:
"Unhandled exception at 0x772718A2 in jsoncpp2.exe: Microsoft C++ exception: nlohmann::detail::type_error at memory location 0x00E9F504."
Let me know why such exception is being generated. When I compile the code, it says no error. However, when I execute it, I get an exception. I have added necessary files and headers.
#include<iostream>
#include<sstream>
#include<nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
std::string greetings = "greetings from string";
json j_string;
j_string["transformation matrix"] = greetings;
auto cpp_string = j_string.get<std::string>();
std::string serialized_string = j_string.dump();
std::cout << serialized_string << '\n';
}
My expected result is:
[
"transfornation matrix" : "greetings from string"
]