Skip to main content
Geraldo Pereira
Associate III
July 23, 2021
Question

STM32 and cJSON problem?

Hi Guys,

I run the following code in a STM32F4429ZI board (a project was generated with STMCube 6.3 and I´m using Cube IDE 1.7):

// Create a json object

cJSON *info;

info=cJSON_CreateObject();

//adding data

cJSON_AddStringToObject(info,"sex","male");

cJSON_AddNumberToObject(info,"age",20);

cJSON_AddStringToObject(info,"name","weijun");

// character pointer

char *jsonStr;

But in the jsonStr only the first two string was added. See bellow:

Name : jsonStr

Details:0x20006e90 "{\n\t\"sex\":\t\"male\",\n\t\"age\":\t"

Default:0x20006e90 "{\n\t\"sex\":\t\"male\",\n\t\"age\":\t"

Decimal:536899216

Hex:0x20006e90

Binary:100000000000000110111010010000

Octal:04000067220

What is wrong? I´ve just tried integrate cJSON in meu projetct today.

Geraldo.

This topic has been closed for replies.

3 replies

Geraldo Pereira
Associate III
July 30, 2021

Hi, my workaround was convert the number to string using sprintf and after, I used the cJSON_AddStringToObject to add the string to cJSON object.

Tilen MAJERLE
ST Employee
July 30, 2021

cJSON uses malloc to allocate memory. Make sure your heap size is big enough

Geraldo Pereira
Associate III
July 30, 2021

Thanks Majerle. I did a lot of tests. But I´ll test and check again.

Thanks.