繁体   English   中英

如何使用cJSON读取字符串

[英]How to read a string with cJSON

我正在使用cJSON,但不知何故我无法使用字符串:

void myfile()
{
  cJSON* type = NULL;
  char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}\n}";
  cJSON * root = cJSON_Parse(text);
  cJSON * format = cJSON_GetObjectItem(root,"format"); 
  int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
  type = cJSON_GetObjectItem(format,"type")->valuestring;
  char * rendered = cJSON_Print(root);
  cJSON_Delete(root);
  printf("rate = %d, type = %s \n", framerate, type) ;
}

我只会垃圾。 我也尝试过,但是它甚至没有编译:

char *type[] = cJSON_GetObjectItem(format,"type")->valuestring;

char text[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}\n}";
cJSON * root   = cJSON_Parse(text);
cJSON * format = cJSON_GetObjectItem(root,"format"); 
cJSON * type   = cJSON_GetObjectItem(format,"type");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
char * rendered = cJSON_Print(root);
printf("%s\n", rendered);
printf("rate = %d, type = %s \n", framerate, type->valuestring);
free(rendered);
cJSON_Delete(root);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM