Easy to read and write the JSON file
In C, you can handle JSON data by using libraries such as cJSON or json-c. Below is a basic example of how to create, write, and read a JSON file using the cJSON library.
Step-by-step:
Install cJSON Library
If you’re using a Linux-based system, you can install cJSON
with:
command
sudo apt-get install libcjson-dev
Manual Installation
Download cJSON:
- Go to the cJSON GitHub repository.
- Download the ZIP file of the repository or clone it:
c
git clone https://github.com/DaveGamble/cJSON.git
Build cJSON:
- Open the cJSON folder and navigate to the
cJSON
directory. - You may need to create a Visual Studio project or a Makefile project depending on your development setup.
- If you are using CMake, you can create a CMake build
c
mkdir build cd build cmake .. cmake --build .
Include cJSON in Your Project:
- After building, ensure that the
cJSON.h
andcJSON.c
files are included in your project. - In your C code, include cJSON as shown
c
#include "cJSON.h"
2. Code to Create and Write JSON
This code demonstrates how to create a JSON object and write it to a file:
c
#include <stdio.h> #include <stdlib.h> #include "cJSON.h" int main() { // Create a JSON object cJSON *root = cJSON_CreateObject(); // Add values to the JSON object cJSON_AddStringToObject(root, "name", "John Doe"); cJSON_AddNumberToObject(root, "age", 30); cJSON_AddStringToObject(root, "occupation", "Software Developer"); // Create an array and add it to the object cJSON *skills = cJSON_CreateArray(); cJSON_AddItemToArray(skills, cJSON_CreateString("C")); cJSON_AddItemToArray(skills, cJSON_CreateString("JavaScript")); cJSON_AddItemToArray(skills, cJSON_CreateString("Python")); cJSON_AddItemToObject(root, "skills", skills); // Print the JSON object as a string char *json_string = cJSON_Print(root); printf("Generated JSON:\n%s\n", json_string); // Write JSON string to a file FILE *file = fopen("data.json", "w"); if (file) { fprintf(file, "%s", json_string); fclose(file); printf("\nJSON saved to data.json\n"); } else { printf("Failed to open file for writing\n"); } // Free memory cJSON_Delete(root); free(json_string); return 0; }
3. Code to Read JSON from a File
The following code reads the JSON file we created:
c
#include <stdio.h> #include <stdlib.h> #include "cJSON.h" void read_json_file(const char *filename) { FILE *file = fopen(filename, "r"); if (!file) { printf("Could not open file %s for reading\n", filename); return; } // Get the file size fseek(file, 0, SEEK_END); long length = ftell(file); fseek(file, 0, SEEK_SET); // Allocate memory to store the content char *content = (char *)malloc(length + 1); fread(content, 1, length, file); fclose(file); content[length] = '\0'; // Null-terminate the string // Parse the JSON content cJSON *root = cJSON_Parse(content); if (!root) { printf("Error parsing JSON file\n"); free(content); return; } // Extract values from the JSON object const cJSON *name = cJSON_GetObjectItem(root, "name"); const cJSON *age = cJSON_GetObjectItem(root, "age"); const cJSON *occupation = cJSON_GetObjectItem(root, "occupation"); printf("Name: %s\n", name->valuestring); printf("Age: %d\n", age->valueint); printf("Occupation: %s\n", occupation->valuestring); // Free the JSON object and memory cJSON_Delete(root); free(content); } int main() { read_json_file("data.json"); return 0; }
Explanation:
- Creating JSON: We use
cJSON_CreateObject()
to create a JSON object, and add data to it usingcJSON_AddStringToObject()
andcJSON_AddNumberToObject()
. - Writing to File: The generated JSON string is written to a file using
fopen()
andfprintf()
. - Reading JSON: We read the file’s contents into memory and parse it using
cJSON_Parse()
. Then we extract values usingcJSON_GetObjectItem()
.
Compilation:
You need to link the cJSON
library when compiling:
c
gcc -o json_example json_example.c -lcjson
Note: If you prefer manual installation, ensure you have the necessary C compiler and development tools installed, such as Visual Studio or MinGW
Popular post
-
Eclipse IDE – Create New Java Project.
Opening the New Java Project…
-
How to start the project in android studio
Android Studio Open the Android…
-
How to use ACOSH function in excel
The ACOSH function returns the…
-
Complete Header tags in html – easy to learn
H tags can be used…
-
Best features in Python programme – easy to learn
Python is the most widely…