我是 json-c 的新手。
我能够以以下格式创建 JSON 对象:
{"loglevel":"INFO", "msg":"Info about car", "timestamp":"actual system time"}
但我需要帮助以以下格式创建 JSON 对象:
{"module":"log","version":1.0, "logs":[{"loglevel":"INFO", "msg":"Info about car", "timestamp":"actual system time"}]}
代码如下:
/*Line is the messge of string which is passed from other module*/
static void json_file(char *line)
{
FILE* lg;
lg = fopen(JSON_FILE,"ab+");
char *json = "{\"key1\":\"data\", \"key2\":1.0, \"Logs\":[";
fwrite(line, sizeof(char), strlen(line),lg);
fputs("]}", lg);
/*replace last ']}' with ',' json string and ]} */
char stringtofind[4] = "]}";
char get_line[2000];
fgets(get_line, sizeof(get_line), lg);
if (strstr(get_line, stringtofind) != NULL)
{
printf("substring found \n");
/* Help Required here*/
/* please help */
// Need to replace the "]}" into ","
}
}