1

我使用 C 语言的 json-c 库。我有以下代码

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json/json.h>

int main()
{
    while(1)
    {
          testJson();
          Sleep(2000); //sleep 2 seconds
    }
    return 0;
}

void testJson()
{
      json_object *new_obj;
      char buf[] = "{ \"foo\": \"bar\", \"foo2\": \"bar2\", \"foo3\": \"bar3\" }"
      new_obj = json_tokener_parse(buf);
      json_object_object_add(new_obj, "d1", json_object_new_string(data1));
      json_object_object_add(new_obj, "d2", json_object_new_string(data2));
      .
      .
      .
      .

      printf("obj=%s",json_object_get_string(new_obj));
      json_object_put(new_obj);
}

它只打印一次 json obj。在它产生分段错误(核心转储)消息之后。但是当我将睡眠时间修改为 10 秒而不是 2 秒时,它工作得很好。

我需要在 2 秒内创建和删除 json 对象。json-c库可以吗?

4

0 回答 0