cjson cJSON_PrintUnformatted return NULL!!!

WayneJia
Posts: 17
Joined: Thu Aug 03, 2017 7:28 am

cjson cJSON_PrintUnformatted return NULL!!!

Postby WayneJia » Tue Oct 24, 2017 9:15 am

I want send json data to server.

My json data sometimes return NULL when run cJSON_PrintUnformatted().Sometimes it run well.
When I add only one array to "data",it works well.But add two or more array,it will return NULL frequently!

Code: Select all

#define SOC_SEND_DATA_ONCE_PACKET_NUM 2//when this value is 1,it works well!
uint8_t mult_build_sensor_data(cJSON *root)
{
	cJSON *j_array;
	uint8_t packet_num = 0;
	soc_send_event_t evt;
	uint8_t temp[1024]={0};

	packet_num = SOC_SEND_DATA_ONCE_PACKET_NUM ;
	printf("mult_build_sensor_data000,packet_num=%d\n",packet_num);
	cJSON_AddStringToObject(root,"type","POSTDATA");
	j_array = cJSON_CreateArray();
	cJSON_AddItemToObject(root,"data",j_array);
	while(packet_num > 0)
	{
		memset(temp,0,1024);
		evt.data = soc_send_t_buf.buf+soc_send_t_buf.head;
		evt.len = (evt.data[3]<<8)+evt.data[4]+UART_PROTOCOL_HEAD_LEN;
		evt.type = SEND_SENSOR_DATA_TO_SERVER;
		evt.data = temp;
		if(get_soc_send_buf_queue(&evt)==ESP_OK)//it will get data of "data" need.
		{
			packet_num--;
			cJSON *d_temp = cJSON_CreateObject();
			cJSON_AddItemToArray(j_array,d_temp);
			build_sensor_data_to_json(d_temp,evt.data);
		}
		else
		{
			printf("Get Queue Error or Empty222, Clear Queue!!!\n");
			clear_soc_send_buf_queue();
			soc_send_buf_queue_resume();
			break;
		}
	}
	printf("mult_build_sensor_data111,packet_num=%d\n",packet_num);
	return packet_num;
}
int send_data_to_server(void)
{
	int socket_ret = ESP_FAIL;
	cJSON *root;
	char *out_buf,*buf;
	uint32_t len = 0,len1 = 0;

	root=cJSON_CreateObject();
	mult_build_sensor_data(root);
	printf("build root success!!!\n");
	out_buf = cJSON_PrintUnformatted(root);
	if(out_buf == NULL)//out buf sometimes return NULL when add two array to "data"!!!
	{
		printf("send_data_to_server root memory error\n");
		cJSON_Delete(root);
		return socket_ret;
	}
	...
	...
	...
}
This is the data format.

Code: Select all

POST /test HTTP/1.1
Host: 192.168.0.34:1001
Content-Type: application/json
Content-Length: 4567

{"type":"POSTDATA","data":[{"accel":[[-1796,44,-3648],[-1796,44,-3648],[-1796,44,-3648],[-1796,44,-3648],[-1796,44,-3648],[-1792,44,-3628],[-1784,48,-3656],[-1788,44,-3624],[-1788,48,-3632],[-1788,44,-3648],[-1788,40,-3612],[-1784,44,-3624],[-1788,48,-3648],[-1784,48,-3636],[-1788,52,-3648],[-1776,48,-3648],[-1780,44,-3632],[-1792,40,-3636],[-1784,48,-3644],[-1788,44,-3648],[-1780,40,-3628],[-1780,44,-3648],[-1784,36,-3648],[-1784,40,-3628],[-1788,32,-3656],[-1784,40,-3620],[-1776,36,-3624],[-1784,36,-3660],[-1776,44,-3632],[-1784,44,-3632],[-1780,44,-3660],[-1784,40,-3632],[-1784,40,-3656],[-1780,40,-3656],[-1784,44,-3640],[-1776,36,-3648],[-1780,48,-3652],[-1780,40,-3644],[-1780,28,-3644],[-1780,36,-3644],[-1776,40,-3660],[-1780,40,-3628],[-1784,36,-3652],[-1776,40,-3636],[-1780,36,-3636],[-1776,36,-3668],[-1784,40,-3632],[-1784,40,-3664],[-1780,40,-3660],[-1784,36,-3620]],"gyro":[[5,16,-9],[-8,1,1],[-5,3,13],[8,-14,-3],[21,-7,-8],[10,24,6],[-1,2,12],[-2,9,-3],[-11,2,-10],[4,-7,-5],[-7,-13,9],[9,3,-11],[12,2,4],[1,-13,-3],[-10,14,-7],[11,4,4],[-9,3,-23],[4,-9,-3],[15,16,-8],[1,3,-4],[-8,-4,-7],[-2,8,-5],[19,23,0],[29,20,3],[7,14,9],[-3,-5,15],[12,3,-5],[-5,2,6],[1,22,3],[10,9,5],[-8,15,-2],[-9,-10,-14],[10,-8,-5],[3,-1,2],[-3,1,-18],[9,-16,1],[14,5,-10],[-5,1,2],[-8,-3,-2],[-7,11,4],[-13,-3,-9],[2,-18,21],[-4,-21,-10],[3,13,1],[-8,-4,-7],[0,5,-4],[-3,0,12],[1,6,5],[9,-5,8],[1,5,20]],"mag":[[240,566,625],[240,566,625],[240,566,625],[240,566,625],[240,566,625],[234,572,619],[234,572,619],[216,554,632],[216,554,632],[246,578,612],[228,566,619],[228,566,619],[228,590,619],[246,584,612],[246,584,612],[216,566,612],[216,566,612],[240,572,612],[234,572,612],[234,572,612],[210,554,612],[210,554,612],[222,572,605],[252,548,598],[252,548,598],[259,566,632],[240,584,592],[240,584,592],[234,578,612],[234,578,612],[216,572,612],[216,572,619],[216,572,619],[222,572,619],[222,572,619],[234,572,605],[234,560,632],[234,560,632],[234,584,619],[234,584,619],[234,572,605],[222,578,598],[222,578,598],[234,554,612],[228,566,626],[228,566,626],[222,566,626],[222,566,626],[222,584,612],[234,560,612]],"temperature":2830,"air_temperature":3515,"air_pressure":101529,"steps":0,"heart":0,"sos":0,"bat":5,"mac":"24:a:c4:82:92:24","APmac":"c8:3a:35:58:35:d0","timestamp":"171024165803"},{"accel":[[-1784,40,-3660],[-1784,40,-3660],[-1776,40,-3628],[-1776,40,-3664],[-1772,36,-3636],[-1776,36,-3652],[-1788,40,-3652],[-1776,40,-3648],[-1784,32,-3652],[-1776,36,-3644],[-1772,36,-3668],[-1776,36,-3640],[-1776,36,-3636],[-1780,40,-3660],[-1780,40,-3624],[-1780,36,-3668],[-1776,40,-3668],[-1772,40,-3640],[-1776,36,-3668],[-1768,32,-3676],[-1776,40,-3644],[-1776,36,-3660],[-1772,32,-3640],[-1780,40,-3652],[-1772,36,-3664],[-1772,32,-3644],[-1776,40,-3644],[-1768,44,-3660],[-1772,36,-3652],[-1772,32,-3648],[-1768,32,-3648],[-1772,36,-3660],[-1764,36,-3648],[-1768,32,-3672],[-1764,28,-3664],[-1772,36,-3648],[-1776,28,-3668],[-1768,32,-3672],[-1768,36,-3648],[-1768,32,-3672],[-1768,36,-3640],[-1764,28,-3648],[-1764,32,-3664],[-1772,28,-3648],[-1768,32,-3664],[-1772,32,-3668],[-1768,28,-3656],[-1764,28,-3664],[-1772,28,-3644],[-1764,24,-3664]],"gyro":[[4,11,14],[0,3,-16],[-2,-5,-10],[-9,15,8],[-20,-8,-5],[13,12,10],[-19,-31,-1],[1,8,10],[-5,-7,13],[-10,9,4],[-5,9,-12],[4,-2,16],[6,-5,-23],[3,5,9],[2,16,-5],[0,1,10],[-9,2,-8],[-2,1,11],[14,13,0],[-17,5,16],[-2,1,-6],[1,-8,12],[8,11,-14],[5,5,-11],[1,5,-1],[4,0,8],[9,19,4],[2,-1,4],[-7,0,-18],[9,5,8],[2,7,28],[2,-7,-6],[-6,-5,11],[3,10,-8],[-1,12,1],[0,-10,7],[1,1,-12],[10,16,-4],[16,12,-2],[-14,-12,0],[-2,4,14],[7,-2,-10],[6,6,3],[0,-3,11],[20,-2,-10],[-7,-19,11],[-21,3,1],[0,22,11],[-3,2,5],[-11,19,1]],"mag":[[216,584,612],[216,584,612],[216,584,612],[240,584,612],[228,566,619],[228,566,619],[246,566,592],[216,578,605],[216,578,605],[234,560,605],[234,560,605],[240,566,612],[228,548,612],[228,548,612],[222,566,612],[222,566,612],[228,566,639],[240,560,598],[240,560,598],[246,560,619],[246,560,619],[234,566,619],[222,560,619],[222,560,619],[234,566,598],[222,566,598],[222,566,598],[222,572,605],[222,572,605],[228,572,626],[228,566,605],[228,566,605],[234,584,612],[234,584,612],[234,572,619],[240,578,612],[240,578,612],[228,572,605],[228,560,598],[228,560,598],[234,554,612],[234,554,612],[228,572,612],[240,566,605],[240,566,605],[234,560,592],[234,560,592],[228,578,612],[246,572,626],[246,572,626]],"temperature":2830,"air_temperature":3530,"air_pressure":101522,"steps":0,"heart":0,"sos":0,"bat":5,"mac":"24:a:c4:82:92:24","APmac":"c8:3a:35:58:35:d0","timestamp":"171024165804"}]}

Who is online

Users browsing this forum: Bing [Bot] and 88 guests