Greetings,
I have a problem regarding parsing JSON after I made a request to an HTTP server (api.thinspeak.com). It says Failed to initialize JSON parser. When I try to debug it, it the jsmn_parse() function exit with value 0. I expect a JSON payload returned from the server as shown in the picture below (when I tried using POSTMAN).
This problem does not happen when I using original http_client demo which uses httpbin.org as the server. The JSON is successfully parsed.
This confused me because both servers returning JSON payload.
I appreciate if someone can help with this. Thank you
Best wishes,
Ikhwan
Code involve:
/*! \brief This function parse JSON data \param[in] ptr - Pointer to JSON data \return 0 on success else -ve \note \warning */ _i32 ParseJSONData(_i8 *ptr) { _i32 retVal = 0; _i32 noOfToken; jsmn_parser parser; jsmntok_t *tokenList; _i8 printBuffer[4]; /* Initialize JSON PArser */ jsmn_init(&parser); /* Get number of JSON token in stream as we we dont know how many tokens need to pass */ noOfToken = jsmn_parse(&parser, (const char *)ptr, strlen((const char *)ptr), NULL, 10); if(noOfToken <= 0) { CLI_Write(" Failed to initialize JSON parser\n\r"); return -1; } /* Allocate memory to store token */ tokenList = (jsmntok_t *) malloc(noOfToken*sizeof(jsmntok_t)); if(tokenList == NULL) { CLI_Write(" Failed to allocate memory\n\r"); return -1; } /* Initialize JSON Parser again */ jsmn_init(&parser); noOfToken = jsmn_parse(&parser, (const char *)ptr, strlen((const char *)ptr), tokenList, noOfToken); if(noOfToken < 0) { CLI_Write(" Failed to parse JSON tokens\n\r"); retVal = noOfToken; } else { CLI_Write(" Successfully parsed "); // sprintf((char *)printBuffer, "%ld", noOfToken); CLI_Write((_u8 *)printBuffer); CLI_Write(" JSON tokens\n\r"); } free(tokenList); return retVal; }