| Connectivity Middleware Utilities Library

JSON parser snippets
Common middleware utilities » JSON parser utilities
#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)
{
case JSON_STRING_TYPE:
{
printf("Found a string: %.*s\n", json_object->value_length,json_object->value );
break;
}
case JSON_NUMBER_TYPE:
{
printf("Found a number: %ld\n", (unsigned long)json_object->intval);
break;
}
case JSON_FLOAT_TYPE:
{
printf("Found a float: %f\n",json_object->floatval);
break;
}
case JSON_BOOLEAN_TYPE:
{
printf("Found a boolean: %d\n",(unsigned int)json_object->boolval);
break;
}
case JSON_NULL_TYPE:
{
printf("Found a NULL type: %.*s\n", json_object->value_length,json_object->value);
break;
}
case JSON_ARRAY_TYPE:
{
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;
}
}