所以我是 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);
但是,我认为它变成了一个字符串。我想要的是将它转换为数组并能够获取每个索引的值。