从C中的JSON中提取字符串

时间:2022-01-07 16:03:14

I have a function which choose and extract a JSON line. This JSON line will be computed in another function which is supposed to extract the value of it.

我有一个选择和提取JSON行的函数。这个JSON行将在另一个函数中计算,该函数应该提取它的值。

"key" : "value",

I wish to extract value without using any library made to manipulate JSON.

我希望在不使用任何用于操作JSON的库的情况下提取值。

My code looks like this so far :

到目前为止我的代码看起来像这样:

char* extractValueInLine(char* line) { 
  if(!verifyLine(line)) // Checks wether line contains 4 quotes or not
    return "";
  int len = strlen( line );
  char* res = NULL;
  unsigned quoteCpt = 0;
  for (unsigned i = 0; i < len; i++){
    if (line[i] == '\"')
        quoteCpt++;
    if (quoteCpt == 3) // If 3 quotes has been skipped
    {
        res = malloc((char) sizeof(res) + sizeof(char)); // Adding memory for one char
        if (NULL == res)
            fprintf(stderr, "Not enough memory.\n");
        else
        {
            char toAppend[2];
            toAppend[1] = '\0';
            toAppend[0] = line[i];
            strcat(res, toAppend); // Append current char 
        }
    }
    return res;
}

This is very ugly and not efficient at all since the output is blank instead of being value. Nevertheless I have tried more and more ways to do this, using string library functions and with sscanf, strtok and stuff but it was either not working or easily breakable.
A code which could work even if a blank space were added or missing would be perfect.
If anyone could advise me or tell me what I am missing here.

这非常难看并且根本不高效,因为输出是空白而不是值。尽管如此,我已经尝试了越来越多的方法,使用字符串库函数和sscanf,strtok和东西,但它要么不工作,要么容易破解。即使添加或丢失空白空间也可以工作的代码是完美的。如果有人可以告诉我或告诉我这里缺少什么。

2 个解决方案

#1


2  

Use strchr() to find the position of the quotes. Then, with that information extract the value.

使用strchr()查找引号的位置。然后,用该信息提取值。

Something like

char *q1 = strchr(line, '\"'); /* needs error checking */
char *q2 = strchr(q1 + 1, '\"');
char *q3 = strchr(q2 + 1, '\"');
char *q4 = strchr(q3 + 1, '\"');

/* allocate (q4 - q3) bytes for res */
sprintf(res, "%.*s", (int)(q4 - q3 - 1), q3 + 1);

#2


1  

The issues with your code

您的代码存在问题

  1. Missing one } which makes your code return in the first iteration itself.
  2. 缺少一个},使你的代码在第一次迭代中返回。

  3. You are allocating new space every time. Which makes it lose the previous contents.
  4. 你每次都在分配新的空间。这使它失去了以前的内容。

The fix would be

修复将是

char* extractValueInLine(char* line) { 
    int len = strlen( line );
    char* res = malloc(1);
    res[0] = '\0';
    unsigned quoteCpt = 0;
    for (unsigned i = 0; i < len; i++){
        if (line[i] == '\"')
            quoteCpt++;
        else if (quoteCpt == 3) {
            res = realloc(res, strlen(res) + sizeof(char) + 1); // Free memory for additional char
            if (NULL == res)
                fprintf(stderr, "Not enough memory.\n");
            else{
                char toAppend[2];
                toAppend[1] = '\0';
                toAppend[0] = line[i];
                strcat(res, toAppend);
            }
        }
    }
    return res;
}

The main change is that I have used realloc instead of malloc which copies the previous contents and frees the old buffer.

主要的变化是我使用了realloc而不是malloc,它复制了以前的内容并释放了旧的缓冲区。

You can see the demo at Ideone

您可以在Ideone上看到演示

#1


2  

Use strchr() to find the position of the quotes. Then, with that information extract the value.

使用strchr()查找引号的位置。然后,用该信息提取值。

Something like

char *q1 = strchr(line, '\"'); /* needs error checking */
char *q2 = strchr(q1 + 1, '\"');
char *q3 = strchr(q2 + 1, '\"');
char *q4 = strchr(q3 + 1, '\"');

/* allocate (q4 - q3) bytes for res */
sprintf(res, "%.*s", (int)(q4 - q3 - 1), q3 + 1);

#2


1  

The issues with your code

您的代码存在问题

  1. Missing one } which makes your code return in the first iteration itself.
  2. 缺少一个},使你的代码在第一次迭代中返回。

  3. You are allocating new space every time. Which makes it lose the previous contents.
  4. 你每次都在分配新的空间。这使它失去了以前的内容。

The fix would be

修复将是

char* extractValueInLine(char* line) { 
    int len = strlen( line );
    char* res = malloc(1);
    res[0] = '\0';
    unsigned quoteCpt = 0;
    for (unsigned i = 0; i < len; i++){
        if (line[i] == '\"')
            quoteCpt++;
        else if (quoteCpt == 3) {
            res = realloc(res, strlen(res) + sizeof(char) + 1); // Free memory for additional char
            if (NULL == res)
                fprintf(stderr, "Not enough memory.\n");
            else{
                char toAppend[2];
                toAppend[1] = '\0';
                toAppend[0] = line[i];
                strcat(res, toAppend);
            }
        }
    }
    return res;
}

The main change is that I have used realloc instead of malloc which copies the previous contents and frees the old buffer.

主要的变化是我使用了realloc而不是malloc,它复制了以前的内容并释放了旧的缓冲区。

You can see the demo at Ideone

您可以在Ideone上看到演示