This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to parse and read JSON file correctly?

Hi guys,

I am using the nRF52832 board and I want to parse and read my JSON file. I don't have too much experience working with cJSON and here you will find my simple code where I try to understand how to define things properly. I will appreciate a lot if you can help me with some advice or better if you can suggest me where I made errors.

Best regards,

Adnan.

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <sys/printk.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"


char *create_hello(void) {
	
	char *string;
	
	cJSON *hello = cJSON_CreateObject();
	
	cJSON *functionName = cJSON_CreateString("hello_world");
	cJSON_AddItemToObject(hello, "functionName", functionName);
	
	string = cJSON_Print(hello);
	
	cJSON_Delete(hello);
	return string;
	
	}

void hello_world (void) {
	
	printk("Hello World! %s\n", CONFIG_BOARD);
	}


void main(void)
{
	const char *hello = "{\"functionName\": \"hello_world\"}";
	cJSON *hello_json = cJSON_Parse(hello);
	cJSON *functionName = cJSON_GetObjectItemCaseSensitive(hello_json, "functionName");
	
	while(true){
		
		if (functionName == "hello_world") {
			hello_world();
			k_sleep(K_SECONDS(1));
			}		
			
		}
}

Related