在C

时间:2019-03-04 21:53:45

标签: 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,但我想尽可能避免这种依赖。如何分隔元素?

1 个答案:

答案 0 :(得分:1)

  

但是,我保留了格式,包括回车符。

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

以下代码段将chunk.memory处的字符串分为head(结果元素之前的部分),res[nres]nres元素的数组)和{{1 }}(结果元素之后,结束tail之后的部分);注释与代码一致。

]