c - 在 C 中将 JSON 数组元素分隔为字符串

标签 c json

我有一个简单的网页,它在 <body> 中显示一个 json 对象.我在这里简化了示例,因为每个元素都有很多数据,并且没有必要在这个问题中打印出来。但是,我保留了格式,包括回车。

<body>
    callBack({
    "resSet":[
        {
        "results":[


{^M
"res":{"data inside res",^M
       "more data",^M
       {"and some more"}^M
},^M

{^M
"res":{"data inside res",^M
       "more data",^M
       {"and some more"}^M
},^M

{^M
"res":{"data inside res",^M
       "more data",^M
       {"and some more"}^M
}],^M
    "other resSet data"^M
}^M
],
})^M
</body>

我在 C 中使用 libcurl 将此数据作为字符串读入内存。我使用 C 和 cURL 来保持可移植性和内存控制。我想要做的是将 "results" 的元素分开数组,这样我就可以根据自己的选择对它们进行重新排序。排序后json写入文件供其他程序使用。

请注意,这可能具有 JSON 对象的结构,但它在内存中被视为字符串。

这是我的代码。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct MemoryStruct {
  char *memory;
  size_t size;
};

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) 
// curl callback function (found on 
https://curl.haxx.se/libcurl/c/getinmemory.html)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;

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

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

  return realsize;
}

int main(int argc, char *argv[]) { // pass query and collection
  curl_global_init(CURL_GLOBAL_ALL);
  CURL *curl;
  CURLcode res;
  struct MemoryStruct chunk;
  char *url = calloc(1024, 1); // url to maxxcat4.astm.org/query4.cgi, plenty of space for query and collection parameters
  char *query = calloc(strlen(argv[1])+1, 1); // +1 so | can be placed in function FetchLine
  char *collection = calloc(strlen(argv[2]), 1);
  char *order; // not allocated, points to return from function FetchLine

  sprintf(query, "%s", argv[1]);
  sprintf(collection, "%s", argv[2]);
  sprintf(url, "http://maxxcat4.astm.org/query4.cgi?query=%s&collection=%s", query, collection); // query then collection

  chunk.memory = malloc(1); // currently using realloc, should change to calloc
  chunk.size = 0; // nothing, initially

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url); // fetch data from url
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,WriteMemoryCallback); // send the data to the function WriteMemoryCallback (found on https://curl.haxx.se/libcurl/c/getinmemory.html)
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); // pass the MemoryStruct chunk to the function
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); // set a user agent if the server requests for one
    res = curl_easy_perform(curl); // retrieve data

    if(res != CURLE_OK) { // make sure things worked
      fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    } else {
      /*
        Sort the data while in memory according to how the file is shuffled
        - read file into memory
        - take one line at a time, breaking on delimiter (if needed)
        - shuffle each res
      */
      // order = FetchLine(query);

      FILE *fp;
      fp = fopen("maxxcat4_data.json", "w");

      /* seperate results array elements here */

      fclose(fp);
      printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
    }
    /* always cleanup */
    curl_easy_cleanup(curl); // clean up handle
    free(chunk.memory);
    free(url);
    free(query);
    free(collection);
    free(order);
    curl_global_cleanup(); // clean up libcurl
  }
  return 0;
}

我的第一个想法是使用 strtok()但我不知道一种方法来分隔字符串,而不是一组分隔符中的单个字符。我读过 json-c ,但我想尽可能避免这样的依赖。如何分离元素?

最佳答案

I kept the format, however, including the carriage returns.

如果您知道确切的格式,则可以利用这些知识并简化阅读 - e。 G。结果的每个元素在一行的开头以 } 结尾。

下面的代码片段将 chunk.memory 中的字符串分成 head(结果元素之前的部分),res[nres] ( nres 元素的数组)和 tail(结果元素之后的部分,在结束 ] 之后);注释与代码内联。

    char c, *head, **res = NULL, *tail; // pointers to parts
    int nres = 0;                       // number of results
    head = chunk.memory;                // it begins here
    const char results[] = "\"results\":[";
    char *cp = strstr(head, results);   // find the results
    if (!cp) printf("%s not found\n", results), exit(1);
    cp += strlen(results);              // skip to the \n
    *cp++ = '\0';                       // delimit the head
    do
    {
        res = realloc(res, sizeof *res * ++nres);
        if (!res) puts("out of memory"), exit(1);
        res[nres-1] = cp;               // store the result
        cp = strstr(cp, "\n}");         // find end of result
        if (!cp) puts("} not found"), exit(1);
        cp += 2;                        // skip over the }
        c = *cp;                        // character after } is , or ]
        *cp++ = '\0';                   // delimit the string
    } while (c == ',');
    if (c != ']') puts("] not found"), exit(1);
    tail = cp;                          // rest follows here
    // output the parts with results rearranged, e. g. backwards
    printf("%s\n", head);
    while (nres--) printf("%s%c", res[nres], nres ? ',' : ']');
    free(res);
    printf("%s", tail);

关于c - 在 C 中将 JSON 数组元素分隔为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992132/

相关文章:

使用多个线程计算素数

c - 相当于 Windows 中的 TIOCOUTQ/SIOCOUTQ

c - 当我以数组名称作为参数调用函数时会发生什么?

javascript - 如何删除/替换 Node16 中的 Unicode 字符

javascript - jQuery 无法将数组作为 JSON 内容类型发送

java - 将 JSON 从 RestTemplate 反序列化为枚举

c - 如何在C中创建一组随机数

c - 理解 libuv/epoll/非阻塞网络 IO

php - 这个 PHP + postgreSQL 代码可以工作吗?我编码盲了!

javascript - 在 HTML 文件中加载 json 文件