gareoke gareoke - 4 years ago 116
JSON Question

Cannot get numbers contained in a JsonArray* using json-glib

I am unable to retrieve values contained in an array response I receive from the server using curl:

struct string data;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); //response from the server is written to data
curl_easy_perform(curl);
dlog_print(DLOG_DEBUG, LOG_TAG, "%s", data.len, data.ptr);


I successfully get the response and the above dlog prints the following:


{"timestamps":[1486371362000,1486371422000,1486371482000]}



I am trying to print out the values in the json array "timestamps" by doing the following:

JsonParser *parser = json_parser_new();
g_type_init();
json_parser_load_from_data(parser, data.ptr, data.len, &error);
JsonNode *root = json_parser_get_root(parser);
JsonObject *obj = json_node_get_object(root);
JsonArray *array = (JsonArray*)json_object_get_array_member(obj,"timestamps");
json_array_foreach_element(timestamps, forEachJsonElement, NULL);


My forEachJsonElement function is defined below:

void forEachJsonElement(JsonArray *array, guint index, JsonNode *element_node, gpointer user_data){
dlog_print(DLOG_DEBUG, LOG_TAG, "forEachJsonElement index: %d", index);
dlog_print(DLOG_DEBUG, LOG_TAG, "forEachJsonElement value: %f", json_node_get_double(element_node));
}


The dlogs print:

forEachJsonElement index: 0
forEachJsonElement value: 0.000000
forEachJsonElement index: 1
forEachJsonElement value: 0.000000
forEachJsonElement index: 2
forEachJsonElement value: 0.000000


My question is, why do these values always print out 0 using json_node_get_double? How can a properly retrieve the values 1486371362000, 1486371422000 and 1486371482000 that I see in the first dlog. I've tried json_node_get_int and everything else I can think of available to me in the json-glib library, but it seems like json_node_get_double always returns 0.00000. I need this so I can properly remove elements that match these values from the original array that the server processed.

Thank you!

MK. MK.
Answer Source

I would try json_node_get_int which returns 64bit int. These dont look like doubles to me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download