I am new to C++ and cannot figure out how to strip some miscellaneous data from a string and then parse it as JSON.
I've ended up using the most documented JSON parser I could find - jansson. It seems excellent, although I'm stuck at the first hurdle.
My program receives a string in the following format:
5::/chat:{"name":"steve","args":[{"connection":"true"}, { "chatbody" : "I am the body" }]}
I've stripped everything outside the curly brackets with:
std::string str=message;
unsigned pos = str.find("{");
std::string string = str.substr (pos);
That leaves:
{
"name": "steve",
"args": [
{
"connection": "true"
},
{
"chatbody": "I am the body"
}
]
}
I'm stuck at stage one parsing this. I have converted the string to a char and then tried to use json_loads, but I don't get anything useful out...
The whole thing looks like this:
void processJson(string message)
{
json_t *root;
json_error_t error;
size_t i;
std::string str=message;
unsigned pos = str.find("{");
std::string str3 = str.substr (pos);
const char * c = str.c_str();
json_t *data, *sha, *name;
root = json_loads(c, 0, &error);
data = json_array_get(root, i);
cout << data;
if(!json_is_object(root))
{
fprintf(stderr, "error: commit data %d is not an object\n", i + 1);
}
}
I need to get the values out, but I just get 01, 02, 03....
is_json_object just says:
error: commit data 1068826 is not an object
error: commit data 1068825 is not an object
error: commit data 1068822 is not an object
What am I doing wrong and how can I properly format this? Ultimately I'll need to iterate over an array but cannot get past this. I'm sure this is just a beginner's mistake.
-EDIT-
Trying to avoid using Boost because of a strict size requirement.