0

所以我是 Tizen Native Development 的新手,在从响应 curl 解析 JSON 数组时需要帮助。我能够使用 curl 发布数据并使用以下代码段检索响应:

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;
}

dlog 将打印:

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

但是,我不知道如何解析内容。我已经阅读了许多关于使用 Json-glib 的主题,但不太了解如何使用它。任何人都可以帮忙吗?

更新:这就是我使用 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");
    }

现在我能够将内容转换为字符:

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

但是,我认为它变成了一个字符串。我想要的是将它转换为数组并能够获取每个索引的值。

4

2 回答 2

0

您可以使用这两个库:

  1. restclient-cpphttps ://github.com/mrtazz/restclient-cpp -
  2. 快速json :http: //rapidjson.org/md_doc_tutorial.html

第一个是 curl 的包装器。它以简单的方式为您提供 GET、POST 等方法。

第二个简化了 json 解析。

这些是 C++ 库,因此您可以编写方法来访问 .cpp 文件中的 api 并使用extern关键字将它们导出到 C

于 2016-11-17T18:20:17.997 回答
0

你需要JSON-GLIB

添加

#include <json-glib.h>

下一步是创建一个解析器实例,如下所示:

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

ParseJsonEntity()示例代码中是执行 JSON 解析的主循环。由于 JSON 具有 Object、Array、Value 三种类型的数据,因此主循环会自动检测并解析其内容。

如果当前节点为 '<strong>object' 或 '<strong>array' 类型,则继续ParseJsonEntity()递归解析 JSON 树。但在 'value' 类型的情况下,它会通过ExtractValue().

请按照本教程了解更多详细信息。那里还提供了示例应用程序。

于 2016-11-09T08:31:25.813 回答