Sample NONOS JSON code anyone?
Posted: Sat Apr 09, 2016 8:55 am
Anyone got some example JSON parsing code that they would be willing to share? Something using the NONOS jsonparse_setup() etc library please.
-->
Open Community Forum for ESP8266, Come share Arduino and IoT (Internet of Things)
https://www.esp8266.com/
#include <stdio.h>
#include "jsonparse.h"
int main(int argc, char *argv[])
{
char string[128];
int json_type;
struct jsonparse_state state;
int units;
int tenths;
jsonparse_setup(&state, data, strlen(data));
while(json_type = jsonparse_next(&state))
{
switch (json_type)
{
case JSON_TYPE_ARRAY:
printf("Array:\n");
break;
case JSON_TYPE_OBJECT:
printf("Object:\n");
break;
case JSON_TYPE_PAIR:
printf("Pair:\n");
break;
case JSON_TYPE_PAIR_NAME:
printf("Pair name: ");
jsonparse_copy_value(&state, string, 128);
printf("%s\n", string);
break;
case JSON_TYPE_STRING:
printf("String: ");
jsonparse_copy_value(&state, string, 128);
printf("%s\n", string);
break;
case JSON_TYPE_INT:
printf("Int: ");
printf("%d\n", jsonparse_get_value_as_int(&state));
break;
case JSON_TYPE_NUMBER:
printf("Number: ");
printf("%ld\n", jsonparse_get_value_as_long(&state));
break;
case JSON_TYPE_ERROR:
printf("Error:\n");
break;
default:
// Turns out these are the ASCII codes for non-whitespace in the JSON
// that is not handled as special case by the parser.
printf("Unknown type: %d\n", json_type);
break;
}
}
return(0);
}