Android 读取和存储json文件代码
时间: 2023-04-05 20:02:32 浏览: 76
Android中Json数据读取与创建的方法
你可以使用以下代码读取和存储json文件:
读取json文件:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_JSON_SIZE 1024
int main() {
FILE *fp;
char json[MAX_JSON_SIZE];
fp = fopen("data.json", "r");
if (fp == NULL) {
printf("Failed to open file\n");
return 1;
}
fgets(json, MAX_JSON_SIZE, fp);
fclose(fp);
printf("%s\n", json);
return 0;
}
```
存储json文件:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_JSON_SIZE 1024
int main() {
FILE *fp;
char json[MAX_JSON_SIZE] = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
fp = fopen("data.json", "w");
if (fp == NULL) {
printf("Failed to open file\n");
return 1;
}
fputs(json, fp);
fclose(fp);
return 0;
}
```
阅读全文