Connectivity Middleware Utilities Library
#include <stdio.h>
#include <string.h>
#include "cy_result.h"
#include "cy_json_parser.h"
char str_snippet[] = " { \"arr\":[1,2,3], \"bool\":true, \"url\": \"https://www.httpbin.org/get\", \"number\": 10000 , \"float\":3.14, \"NULL_VAL\":null}";
static cy_rslt_t parse_json_snippet_callback (cy_JSON_object_t* json_object, void *arg )
{
switch(json_object->value_type)
{
{
printf("Found a string: %.*s\n", json_object->value_length,json_object->value );
break;
}
{
printf("Found a number: %ld\n", (unsigned long)json_object->intval);
break;
}
{
printf("Found a float: %f\n",json_object->floatval);
break;
}
{
printf("Found a boolean: %d\n",(unsigned int)json_object->boolval);
break;
}
{
printf("Found a NULL type: %.*s\n", json_object->value_length,json_object->value);
break;
}
{
printf("Found an ARRAY");
break;
}
default:
{
break;
}
}
return 0;
}
int json_parser_snippet(void)
{
cy_JSON_parser_register_callback ( parse_json_snippet_callback, NULL );
if (cy_JSON_parser(str_snippet , strlen(str_snippet)) == CY_RSLT_SUCCESS)
{
printf("Successfully parsed the JSON input\n");
return 0;
}
else
{
printf("Failed to parse the JSON input\n");
return -1;
}
}