You've already forked M5Stack_Linux_Libs
mirror of
https://github.com/m5stack/M5Stack_Linux_Libs.git
synced 2026-05-20 11:01:38 -07:00
cjson_demo
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
|
||||
dist
|
||||
build
|
||||
.config.mk
|
||||
.flash.conf.json
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
from pathlib import Path
|
||||
import os
|
||||
with open(str(Path(os.getcwd())/'..'/'..'/'tools'/'scons'/'project.py')) as f:
|
||||
exec(f.read())
|
||||
@@ -0,0 +1,8 @@
|
||||
# unix
|
||||
# CONFIG_TOOLCHAIN_PATH="/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin"
|
||||
# win
|
||||
# CONFIG_TOOLCHAIN_PATH="..\\gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf\\bin"
|
||||
|
||||
# CONFIG_TOOLCHAIN_PREFIX="arm-linux-gnueabihf-"
|
||||
|
||||
CONFIG_CJSON_ENABLED=y
|
||||
@@ -0,0 +1,33 @@
|
||||
# project_root/src/SConscript
|
||||
import os
|
||||
# Import the environment from the SConstruct file
|
||||
Import('env')
|
||||
with open(env['PROJECT_TOOL_S']) as f:
|
||||
exec(f.read())
|
||||
|
||||
|
||||
SRCS = Glob('src/*.c*')
|
||||
INCLUDE = [ADir('include'), ADir('.')]
|
||||
PRIVATE_INCLUDE = []
|
||||
REQUIREMENTS = ['pthread', 'cjson']
|
||||
STATIC_LIB = []
|
||||
DYNAMIC_LIB = []
|
||||
DEFINITIONS = []
|
||||
DEFINITIONS_PRIVATE = []
|
||||
LDFLAGS = []
|
||||
LINK_SEARCH_PATH = []
|
||||
|
||||
|
||||
env['COMPONENTS'].append({'target':env['PROJECT_NAME'],
|
||||
'SRCS':SRCS,
|
||||
'INCLUDE':INCLUDE,
|
||||
'PRIVATE_INCLUDE':PRIVATE_INCLUDE,
|
||||
'REQUIREMENTS':REQUIREMENTS,
|
||||
'STATIC_LIB':STATIC_LIB,
|
||||
'DYNAMIC_LIB':DYNAMIC_LIB,
|
||||
'DEFINITIONS':DEFINITIONS,
|
||||
'DEFINITIONS_PRIVATE':DEFINITIONS_PRIVATE,
|
||||
'LDFLAGS':LDFLAGS,
|
||||
'LINK_SEARCH_PATH':LINK_SEARCH_PATH,
|
||||
'REGISTER':'project'
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "cJSON.h"
|
||||
|
||||
int main() {
|
||||
// Create a JSON object
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(root, "name", "John Doe");
|
||||
cJSON_AddNumberToObject(root, "age", 30);
|
||||
cJSON_AddBoolToObject(root, "employed", 1);
|
||||
|
||||
// Create an array and add it to the JSON object
|
||||
cJSON *skills = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(skills, cJSON_CreateString("C"));
|
||||
cJSON_AddItemToArray(skills, cJSON_CreateString("C++"));
|
||||
cJSON_AddItemToArray(skills, cJSON_CreateString("Python"));
|
||||
cJSON_AddItemToObject(root, "skills", skills);
|
||||
|
||||
// Convert the JSON object to a string and print it
|
||||
char *json_string = cJSON_Print(root);
|
||||
printf("Generated JSON:\n%s\n", json_string);
|
||||
|
||||
// Parse the JSON string
|
||||
cJSON *parsed_root = cJSON_Parse(json_string);
|
||||
if (parsed_root == NULL) {
|
||||
fprintf(stderr, "Error parsing JSON string.\n");
|
||||
} else {
|
||||
// Get data from the parsed JSON object
|
||||
cJSON *name = cJSON_GetObjectItem(parsed_root, "name");
|
||||
cJSON *age = cJSON_GetObjectItem(parsed_root, "age");
|
||||
cJSON *employed = cJSON_GetObjectItem(parsed_root, "employed");
|
||||
cJSON *parsed_skills = cJSON_GetObjectItem(parsed_root, "skills");
|
||||
|
||||
// Print the parsed data
|
||||
printf("Parsed JSON:\n");
|
||||
printf("Name: %s\n", name->valuestring);
|
||||
printf("Age: %d\n", age->valueint);
|
||||
printf("Employed: %s\n", employed->valueint ? "true" : "false");
|
||||
|
||||
printf("Skills:\n");
|
||||
int skill_count = cJSON_GetArraySize(parsed_skills);
|
||||
for (int i = 0; i < skill_count; i++) {
|
||||
cJSON *skill = cJSON_GetArrayItem(parsed_skills, i);
|
||||
printf(" - %s\n", skill->valuestring);
|
||||
}
|
||||
}
|
||||
|
||||
// Free memory
|
||||
cJSON_Delete(root);
|
||||
cJSON_Delete(parsed_root);
|
||||
free(json_string);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user