0

So I'm new to Tizen Native Development and needing help in parsing JSON array from the response curl. I was able to post data using curl and retrieve the response using this snippet:

static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp){
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;

mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
  /* out of memory! */
  dlog_print(DLOG_DEBUG, TAG, "not enough memory (realloc returned NULL)\n");
  return 0;
  }

  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;
  dlog_print(DLOG_DEBUG, TAG, contents);

  return realsize;
}

the dlog will print:

["423866","423865","423864","423862","423861","423856","423855","423851","423846","423844"]

However, I can't figure out how to parse the contents. I've read many topics on using Json-glib, but can't quite understand how to use it. Can anyone help?

Update: This is how I post data using curl.

/* get a curl handle */
curl = curl_easy_init();

if(curl) {
    dlog_print(DLOG_DEBUG, TAG, "curl init");
    dlog_print(DLOG_DEBUG, TAG, "curl url: %s", url);
  /* Set CURL parameters */
    /* First set the URL that is about to receive our POST. This URL can
    just as well be a https:// URL if that is what should receive the
    data. */
    curl_easy_setopt(curl, CURLOPT_URL, url);

    /* send all data to this function  */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "w=4");

    /* we pass our 'chunk' struct to the callback function */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

    /* some servers don't like requests that are made without a user-agent
     field, so we provide one */
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
        dlog_print(DLOG_DEBUG, TAG, "curl_easy_perform() Failed: %s\n",curl_easy_strerror(res));
    else{
        dlog_print(DLOG_DEBUG, TAG, "curl_easy_perform() Success");
        dlog_print(DLOG_DEBUG, TAG, "curl_code: %d", res);
        dlog_print(DLOG_DEBUG, TAG, "%d bytes retrieved\n", (long)chunk.size);
    }

    /* always cleanup */
    curl_easy_cleanup(curl);
    }
else{
    dlog_print(DLOG_DEBUG, TAG, "curl failed");
    }

Now I was able to convert contents to char:

char* test;
test = (char*) contents;
dlog_print(DLOG_DEBUG, TAG, "test value: %s", test);

However, I think it became as a string. What I want is to convert it to array and be able to get the values per index.

Christian Callelero
  • 846
  • 1
  • 8
  • 17
  • You have provided the procedure for getting the JSON. What did you try for parsing the JSON ? – Iqbal hossain Nov 08 '16 at 08:26
  • Is that the complete JSON value? If it is, it is an array, but not a JSON Array. – AL. Nov 08 '16 at 10:06
  • @AL. yes its the complete JSON value. I'm having a hard time in the data type as you can see contents data type is void. So I don't know how to convert it to JSON array or to a simple array. – Christian Callelero Nov 09 '16 at 06:24

2 Answers2

0

You need JSON-GLIB.

Add

#include <json-glib.h>

Next step is to create a parser instance as following:

JsonParser *jsonParser  =  NULL;
GError *error  =  NULL;   
jsonParser = json_parser_new ();

ParseJsonEntity() in the sample code is the main loop to execute JSON parsing. As JSON has data as three type like Object, Array, Value, the main loop intenally detect it and parse its content.

If the current node is ‘object’ or ‘array’ type, it continue to parse JSON tree by ParseJsonEntity() recursively. but in case of ‘value’ type, it extract values by ExtractValue().

Follow this tutorial for more details. Sample app is also given there.

Iqbal hossain
  • 1,778
  • 3
  • 17
  • 24
0

You can use this two libs:

  1. restclient-cpp: https://github.com/mrtazz/restclient-cpp -
  2. rapidjson: http://rapidjson.org/md_doc_tutorial.html

The first one is a wrapper for curl. It gives you the GET, POST, etc, methods in a simple way.

The second one simplifies the json parsing.

These are C++ libs so you can write your methods to access the api in a .cpp file and export them to C with the extern keyword

peterg
  • 71
  • 5