Pagini recente » Cod sursa (job #1361057) | Cod sursa (job #1759175) | Cod sursa (job #1350379) | Cod sursa (job #2913871) | Cod sursa (job #1359764)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RETARDED " '\"()\n "
void parseJsonObject(char *object)
{
char *value;
char *p, *q, *r;
p = strtok(object, ",");
while (p)
{
q = strchr(p, ':');
q++;
while (strchr(RETARDED, *q))
{
q++;
}
r = q + strlen(q);
while (strchr(RETARDED, *r))
{
r--;
}
r++;
value = calloc(r - q + 1, sizeof(char));
strncpy(value, q, r - q);
printf("%s,", value);
free(value);
p = strtok(NULL, ",");
}
printf("\n");
}
void printKeys(char *object)
{
char *cpy = calloc(strlen(object) + 1, sizeof(char));
strcpy(cpy, object);
char *key;
char *p, *q, *r;
p = strtok(cpy, ",");
while (p)
{
q = p;
while (strchr(RETARDED, *q))
{
q++;
}
r = strchr(q, '"');
key = calloc(r - q + 1, sizeof(char));
strncpy(key, q, r - q);
printf("%s,", key);
free(key);
p = strtok(NULL, ",");
}
free(cpy);
printf("\n");
}
int main(void)
{
FILE *jsonFile = fopen("convertor.in", "r");
freopen("convertor.out", "w", stdout);
char c;
char *jsonObject = calloc(0, sizeof(char);
int l = 0;
int o = 0;
int pK = 0;
while ((c = fgetc(jsonFile)))
{
if (c == EOF)
{
break;
}
if (c == '[' || c == ']')
{
continue;
}
else if (c == '{')
{
o = 1;
l = 0;
jsonObject = calloc(1, sizeof(char));
}
else if (c == '}')
{
o = 0;
jsonObject[l] = 0;
if (pK == 0)
{
printKeys(jsonObject);
pK = 1;
}
parseJsonObject(jsonObject);
free(jsonObject);
jsonObject = NULL;
}
else if (o == 1)
{
jsonObject[l++] = c;
jsonObject = realloc(jsonObject, l + 1);
}
}
fclose(jsonFile);
return 0;
}