-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.c
127 lines (112 loc) · 3.47 KB
/
json_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h"
void ParseJSON(cJSON *cont)
{
char *p = NULL;
p = cJSON_Print(cont);
printf("parse: \n%s\n\n", p);
free(p);
p = NULL;
return;
}
void DelJSON(cJSON *cont)
{
if (NULL != cont)
{
cJSON_Delete(cont);
cont = NULL;
}
return;
}
int main()
{
int i;
int plmnNum;
FILE *fp;
long len;
char *content;
cJSON *root;
cJSON *isp_arr;
cJSON *item;
cJSON *plmnItem;
cJSON *freqItem;
cJSON *cfreqItem;
cJSON *commonP;
cJSON *plmnArray;
cJSON *testArray;
fp = fopen("./json_test.txt", "rw");
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
content = (char *)malloc(len + 1);
fread(content, 1, len, fp);
fclose(fp);
root = cJSON_Parse(content);
if (!root)
{
printf("json error\n");
return 0;
}
ParseJSON(root);
isp_arr = cJSON_GetObjectItem(root, "opDeviceParameter");
if (NULL == isp_arr)
{
printf("opDeviceParameter is null\n");
return 0;
}
freqItem = cJSON_GetObjectItem(isp_arr, "Frequency");
plmnItem = cJSON_GetObjectItem(freqItem, "plmn");
plmnNum = cJSON_GetArraySize(plmnItem);
for (i = 0; i < plmnNum; i++)
{
item = cJSON_GetArrayItem(plmnItem, i);
printf("%s\n", item->valuestring);
}
DelJSON(root);
root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "code", "000000");
cJSON_AddStringToObject(root, "msg", "Success");
cJSON_AddItemToObject(root, "opDeviceParameter", item = cJSON_CreateObject());
cJSON_AddStringToObject(item, "operationType", "M");
cJSON_AddItemToObject(item, "commonParameter", commonP = cJSON_CreateObject());
cJSON_AddNumberToObject(commonP, "tacPeriod", 120);
cJSON_AddNumberToObject(commonP, "bandWidth", 3);
cJSON_AddNumberToObject(commonP, "tac", 238);
cJSON_AddNumberToObject(commonP, "redirected_earfcn", 38400);
cJSON_AddNumberToObject(commonP, "syncMode", 0);
cJSON_AddNumberToObject(commonP, "imsiReportInterval", 0);
cJSON_AddNumberToObject(commonP, "radioSwitch", 1);
cJSON_AddItemToObject(item, "Frequency", cfreqItem = cJSON_CreateObject());
cJSON_AddNumberToObject(cfreqItem, "upFrequency", 38950);
cJSON_AddNumberToObject(cfreqItem, "downFrequency", 38950);
cJSON_AddNumberToObject(cfreqItem, "priority", 0);
cJSON_AddNumberToObject(cfreqItem, "rsrp", 0);
cJSON_AddNumberToObject(cfreqItem, "pci", 5);
char plmns[5][7] = {0};
char *plmnsptr[20];
int inttestarray[20] = {-1, 0, 1, -2, -3};
int intNum = 5;
char *jsonStr = NULL;
plmnNum = 3;
for (i = 0; i < plmnNum; i++)
{
plmnsptr[i] = plmns[i];
// strcpy(plmns[i], "460.01");
sprintf(plmns[i], "460.0%d", i);
}
cJSON_AddItemToObject(cfreqItem, "plmn", plmnArray = cJSON_CreateStringArray((const char **)plmnsptr, plmnNum));
// cJSON_AddItemToObject(cfreqItem, "array_test", testArray = cJSON_CreateIntArray((const int *)inttestarray, intNum));
cJSON_AddItemToObject(cfreqItem, "array_test", testArray = cJSON_CreateIntArray(inttestarray, intNum));
cJSON_AddNumberToObject(cfreqItem, "frameOffset", 0);
cJSON_AddNumberToObject(cfreqItem, "powerLevel", 0);
jsonStr = cJSON_Print(root);
printf("jsonStr: \n%s\n", jsonStr);
DelJSON(root);
free(jsonStr);
return 0;
}