I need to parse a small JSON file on an embedded system (only 10K RAM/flash). The JSON is:
{
"data1":[1,2,3,4,5,6,7,8,9],
"data2":[
[3,4,5,6,1],
[8,4,5,6,1],
[10,4,5,3,61],
[3,4,5,6,1],
[3,4,5,6,1],
[3,4,5,6,1]
]}
jsmn looks great to fit the requirement, but it's not like most JSON parsers, since it only gives you tokens. I tried, but could not figure it out.
Could someone share an example of how to parse it with jsmn?
jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You can find more information about JSON format at json.org. Library sources are available at https://github.com/zserge/jsmn.
parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
We released simdjson 0.3: the fastest JSON parser in the world is even better! Last year (2019), we released the simjson library. It is a C++ library available under a liberal license (Apache) that can parse JSON documents very fast.
Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.
You can use tiny-json. It gives you more than tokens. I have used with microcontrollers of 16-bits and 32-bits and it works fine.
char str[] = "{"
"\"data1\":[1,2,3,4,5,6,7,8,9],"
"\"data2\":["
"[3,4,5,6,1],"
"[8,4,5,6,1],"
"[10,4,5,3,61],"
"[3,4,5,6,1],"
"[3,4,5,6,1],"
"[3,4,5,6,1]"
"]"
"}";
puts( str );
json_t pool[64];
json_t const* root = json_create( str, pool, 64 );
json_t const* data1 = json_getProperty( root, "data1" );
if ( data1 && JSON_ARRAY == json_getType( data1 ) ) {
json_t const* field = json_getChild( data1 );
while( field ) {
if ( JSON_INTEGER == json_getType( field ) ) {
long long int data = json_getInteger( field );
printf("Integer from data1: %lld\n", data );
}
field = json_getSibling( field );
}
}
json_t const* data2 = json_getProperty( root, "data2" );
if ( data2 && JSON_ARRAY == json_getType( data2 ) ) {
json_t const* array = json_getChild( data2 );
while( array ) {
if ( JSON_ARRAY == json_getType( array ) ) {
puts("Array in data2");
json_t const* field = json_getChild( array );
while( field ) {
if ( JSON_INTEGER == json_getType( field ) ) {
long long int data = json_getInteger( field );
printf("Integer from array of data2: %lld\n", data );
}
field = json_getSibling( field );
}
}
array = json_getSibling( array );
}
}
I've just created jsmnRipper. This code lets you extract the tokens parsed with jsmn in a very simple way.
As an example here is the result of parsed the JSON message returned by ACRCloud
You can find the code for doing that at https://github.com/ipserc/jsmnRipper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With