2016-09-20 12:42 AM
Hello all,
im going to parse json string in my project, i found jansson library for keil, there is an example for cortex-m3 that run just fine in simulator. but the problem is when run the code on stm32f103 the output is worng, please see below: the example code:void
add_2array_to_json( json_t* obj,
const
char
* name,
const
int
*
marr,
size_t
dim1,
size_t
dim2 )
{
size_t
i, j;
json_t* jarr1 = json_array();
for
( i=0; i<dim1; ++i ) {
json_t* jarr2 = json_array();
for
( j=0; j<dim2; ++j ) {
int
val = marr[ i*dim2 + j ];
json_t* jval = json_integer( val );
json_array_append_new( jarr2, jval );
}
json_array_append_new( jarr1, jarr2 );
}
json_object_set_new( obj, name, jarr1 );
return
;
}
void
SystemClock_Config(
void
);
int
main()
{
json_t* jdata;
char
* s;
int
arr1[2][3] = { {1,2,3}, {4,5,6} };
int
arr2[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} };
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
MX_USART2_UART_Init();
jdata = json_object();
add_2array_to_json( jdata,
''arr1''
, &arr1[0][0], 2, 3 );
add_2array_to_json( jdata,
''arr2''
, &arr2[0][0], 4, 4 );
s = json_dumps( jdata, 0 );
HAL_UART_Transmit(&huart1,(uint8_t*)s,
strlen
(s),1000);
HAL_UART_Transmit(&huart1,
''Hello''
,5,1000);
// puts( s );
free
( s );
json_decref( jdata );
while
(1);
}
the out put i get is just:
{}
correct simulator output is:
{''arr1'': [[1, 2, 3], [4, 5, 6]], ''arr2'': [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]}
#json-keil-uvision-stm32f-parse
2017-12-05 07:35 AM
In case someone comes across this old question in a search...
I spotted this the other day:
JSMNjsmn (pronounced like ‘jasmine’) is a minimalistic JSON parser in C. It can be easily integrated into the resource-limited projects or embedded systems.
You can find more information about JSON format at
.Library sources are available at
https://github.com/zserge/jsmn
.Haven't had a chance to try it myself yet ...