JSON Night Crue

Hi everyone,
I am trying to use JSON Night Crue library and I have some problems. My code to test was based in revision " 7:8aa4d0e98eb0" “Added a very basic example in documentation”. When I run the code below, the jsonSource is not recognized as valid JSON even when it is the same as the example and validate with external JSON tools. Which could be the problem?

#include "mbed.h"
#include "Json.h"

// main() runs in its own thread in the OS
int main()
{
    const char *jsonSource = "{\"team\":\"Night Crue\",\"company\":\"TechShop\",\"city\":\"San 
Jose\",\"state\":\"California\",\"country\":\"USA\",\"zip\":95113,\"active\":true,\"members\": 
[{\"firstName\":\"John\",\"lastName\":\"Smith\",\"active\":false,\"hours\":18.5,\"age\":21}, 
{\"firstName\":\"Foo\",\"lastName\":\"Bar\",\"active\":true,\"hours\":25,\"age\":21}]}";
    Json json(jsonSource, strlen(jsonSource));
    char printTest[32];
    int countTokens = json.parsedTokenCount();
    printf("Test JSON \n");
    printf("count tokens %d\n", countTokens);
    if (!json.isValidJson())
    {
        printf("Invalid JSON: %s\n", jsonSource);
    }
    else 
    {
        printf("Valid JSON \n");
    }

    if ( json.type(0) != JSMN_OBJECT )
    {
         printf("Invalid JSON.  ROOT element is not Object: %s\n", jsonSource);
    }
    else 
    {
        printf("ROOT element is an object \n");
    }

    while (true)
    {

    }
}

Output obtained through terminal:

Test JSON
count tokens -1
Invalid JSON: {“team”:“Night Crue”,“company”:“TechShop”,“city”:“San Jose”,“state”:“California”,“country”:“USA”,“zip”:95113,“active”:true,“members”:[{“firstName”:“John”,“lastName”:“Smith”,“active”:false,“hours”:18.5,“age”:21},{“firstName”:“Foo”,“lastName”:“Bar”,“active”:true,“hours”:25,“age”:21}]}
Invalid JSON. ROOT element is not Object: {“team”:“Night Crue”,“company”:“TechShop”,“city”:“San Jose”,“state”:“California”,“country”:“USA”,“zip”:95113,“active”:true,“members”:[{“firstName”:“John”,“lastName”:“Smith”,“active”:false,“hours”:18.5,“age”:21},{“firstName”:“Foo”,“lastName”:“Bar”,“active”:true,“hours”:25,“age”:21}]}

Thanks for your help

The problem was the default number of tokens (32) and jsonSource has 39 tokens. I updated the amount of tokes and it works well.

1 Like