Convert an integer array to json in c

wuampa Source

I have a buffer which is filled by an external hardware (DMA). When this happens, I can print the data using the following code:

void* buffer;
/*buffer is initialized and filled at some point*/
for (k=0; k<10; k++) {
    printf("%d ", ((unsigned int*) buffer)[k]);

I would like to put this data into JSON format using the functions provided by libjson. This is my attempt:

json_object * jobj = json_object_new_object();

json_object *jarray = json_object_new_array();

if (!jobj || !jarray) {
    return NULL;
for (k=0; k<10; k++) {
    printf("%d ", (((unsigned int*) buffer);
    sprintf(str, "%d", ((unsigned int*) buffer)[k]);
    json_object *jstring = json_object_new_string(str);
json_object_object_add(jobj,"array", jarray);

This code works but I'm not sure if so many definitions of "json_object *jstring" may be correct, especially for large amounts of data.



comments powered by Disqus