(json-c学习1) linux c语言解析json数组(纯代码)

 1 #include "json.h"
  2 #include 
  3 #include 
  4 #include 
  5
  6 #define OR_GROUP "group"
  7 #define LOOP_NUMBER "loop_number"
  8 #define SENSOR_ADDRESS "sensor_address"
  9 #define BODY  "body"
 10 #define MODULE_ID "module_id"
 11
 12 char *TestBodyFunction(void)
 13 {
 14     int len = 0;
 15     char *jsonMsgBuf = NULL;
 16     char *str = "{\"group\":1, \"loop_number\":1, \"sensor_address\":1, \"body\":[{\"loop_number\":1, \"module_id\":2}, \
 17 {\"loop_number\":1, \"module_id\":3}, {\"loop_number\":1, \"module_id\":4}]}";
 18     len = strlen(str);
 19     printf("len = %d\n", len);
 20     printf("str = %s\n", str);
 21
 22     jsonMsgBuf = (char *)malloc(len+1);
 23     memset(jsonMsgBuf, 0, len+1);
 24     memcpy(jsonMsgBuf, str, len);
 25     return jsonMsgBuf;
 26 }
 27
 28 //struct json_object *TestArrayFunction(void)
 29 char *TestArrayFunction(void)
 30 {
 31     int len = 0;
 32     //struct json_object *jsonMsgBuf = NULL;
 33     char *jsonMsgBuf = NULL;
 34     char *str = "[{\"loop_number\":1, \"sensor_address\":2}, {\"loop_number\":1, \"sensor_address\":3}, {\"loop_number\":1,     \"sensor_address\":4}]";
 35     len = strlen(str);
 36     printf("len = %d\n", len);
 37     printf("str = %s\n", str);
 38
 39     //jsonMsgBuf = (struct json_object*)malloc(len+1);
 40     jsonMsgBuf = (char *)malloc(len+1);
 41     if (jsonMsgBuf == NULL)
 42     {
 43         printf("failed to malloc!\n");
 44     }
 45     memset(jsonMsgBuf, 0, len+1);
 46     memcpy(jsonMsgBuf, str, len);
 47     return jsonMsgBuf;
 48 }
 49
 50 int ParsingBodyFunction(char *jsonMsgBuf)
 51 {
	//解析内容格式:
	/*
	{
		"group":1,
		"loop_number":1,
		"sensor_address":1,
		"body": 
		[
			{"loop_number":1, "module_id":1},
			{"loop_number":1, "module_id":2},
			{"loop_number":1, "module_id":3},
			...
		]
	}
	*/
 52     int groupId = 0;
 53     int ln = 0;
 54     int sd = 0;
 55     int count = 0;
 56     int lnn = 0;
 57     int mi = 0;
 58     int i = 0;
 59     json_object *pstObj = NULL;
 60     json_object *array = NULL;
 61     json_object *arrayObj = NULL;
 62     pstObj = json_tokener_parse(jsonMsgBuf);
 63     if (NULL == pstObj)
 64     {
 65         printf("%s : json_tokener_parse failed.\n", __FUNCTION__);
 66     }
 67     else
 68     {
 69        json_object_object_foreach(pstObj, key1, val1)
 70        {
 71             if (0 == strcmp(key1, OR_GROUP))
 72             {
 73                 groupId = json_object_get_int(val1);
 74                 printf("groupId = %d\n", groupId);
 75             }
 76             else if (0 == strcmp(key1, LOOP_NUMBER))
 77             {
 78                 ln = json_object_get_int(val1);
 79                 printf("ln = %d\n", ln);
 80             }
 81             else if (0 == strcmp(key1, SENSOR_ADDRESS))
 82             {
 83                 sd = json_object_get_int(val1);
 84                 printf("sd = %d\n", sd);
 85             }
 86             else if (0 == strcmp(key1, BODY))
 87             {
 88                 count  = json_object_array_length(val1);
 89                 printf("count = %d\n", count);
 90                                 //array = json_object_get_array(val1);
 91                 for (i = 0; i < count; i++)
 92                 {
 93                     arrayObj = json_object_array_get_idx(val1, i);
 94                     json_object_object_foreach(arrayObj, key2, val2)
 95                     {
 96                         if (0 == strcmp(key2, LOOP_NUMBER))
 97                         {
 98                             lnn = json_object_get_int(val2);
 99                             printf("lnn = %d\n", lnn);
100                         }
101                         else if (0 == strcmp(key2, MODULE_ID))
102                         {
103                             mi = json_object_get_int(val2);
104                             printf("mi = %d\n", mi);
105                         }
106                     }
107                }
108             }
109         }
110     }
111 }
112
113 //单个传感器id
114 typedef struct _SinglePointIdStruct
115 {
116         int loopNumber;
117         int sensorAddress;
118 } SinglePointIdStruct;
119
120 //int ParsingArrayFunction(struct json_object *jsonMsgBuf)
121 int ParsingArrayFunction(char *jsonMsgBuf)
122 {
	//解析内容格式:
    /*
	[
		{"loop_number": 1,  "sensor_address":1},
		{"loop_number": 1,  "sensor_address":1},
		{"loop_number": 2,  "sensor_address":1},
		{"loop_number": 2,  "sensor_address":1},
		…
	]
	*/
123     int i = 0;
124     int count = 0;
125     struct json_object *arrayObj = NULL;
126     json_object *array = NULL;
127
128                 // struct array_list *array = json_object_get_array(jsonMsgBuf);
129     //struct json_object *array = jsonMsgBuf;
130     array = json_tokener_parse(jsonMsgBuf);
131     int ln = 0;
132     int sd = 0;
133     SinglePointIdStruct *moduleArray = NULL;
134     if (NULL == array)
135     {
136         printf("%s : json_tokener_parse failed.\n", __FUNCTION__);
137         return -1;
138     }
139     else
140     {
141         count = json_object_array_length(array);
142         printf("array count = %d\n", count);
143         moduleArray = (SinglePointIdStruct*)malloc(count*sizeof(SinglePointIdStruct));
144         if (moduleArray == NULL)
145         {
146             printf("%s malloc failed!\n");
147             return -2;
148         }
149         for (i = 0; i < count; i++)
150         {
151             arrayObj = json_object_array_get_idx(array, i);
152             json_object_object_foreach(arrayObj, key1, val1)
153             {
154                 if (0 == strcmp(key1, LOOP_NUMBER))
155                 {
156                     ln = json_object_get_int(val1);
157                     printf("array ln = %d\n", ln);
158                 }
159                 else if (0 == strcmp(key1, SENSOR_ADDRESS))
160                 {
161                     sd = json_object_get_int(val1);
162                     printf("array sd = %d\n", sd);
163                 }
164                         //AllSensorInfoArray[ln-1][sd-1].startStopFlag = 1;  //表示要启动
165             }
166         }
167     }
168 }
169
170 int main(void)
171 {
172     char *body = TestBodyFunction();
173     ParsingBodyFunction(body);
174     printf("=====================================================\n");
175     //struct json_object *array = TestArrayFunction();
176     char *array = TestArrayFunction();
177     ParsingArrayFunction(array);
178     return 0;
179 }
180
181
                                                                                                          

需要这些头文件(到json库中去找):

运行结果:

(json-c学习1) linux c语言解析json数组(纯代码)_第1张图片

你可能感兴趣的:(json-c学习之路)