英文:
Extracting Key from nested array JSON using cJSON
问题
我需要从嵌套的数组JSON中提取关键信息。
嵌套的C JSON
{
"SDCERR": 0, "InfoMsg": "", "count": 18,
"connections": {
"b59519e0-917d-4c29-b31a-81e1837bd43e": {"activated": 0, "id": "PEAP_GTC_NO_CA_CERT", "type": ""},
"518b71d4-28ca-41c5-abb6-88245f0bf3fb": {"activated": 0, "id": "Manoj", "type": ""},
"91988884-d524-43a2-b255-88817330fd8b": {"activated": 0, "id": "NaveedP", "type": ""},
"26dfa985-cea3-4138-b03e-ab25f96e1dbb": {"activated": 0, "id": "PEAP_MSCHAPv2_NO_CA_CERT", "type": ""},
"0d82d7d3-a00a-45e3-a42b-66b13afc03f5": {"activated": 0, "id": "PEAP_MSCHAPv2_CA_CERT", "type": ""},
"1bb69f6c-d75e-44ad-a6ee-de7a5095b57e": {"activated": 0, "id": "EAP_TLS_NO_CA_CERT", "type": ""},
"566d673a-b48b-4e82-aaa5-724c61ed54c0": {"activated": 0, "id": "nagaveni", "type": ""},
"9f382a6d-8e3e-4a7e-88e3-33f0b885dcd0": {"activated": 0, "id": "EAP_TTLS_NO_CA_CERT", "type": ""},
"8284b1b8-a268-4a99-88e4-fdf35f6badb0": {"activated": 0, "id": "PSK", "type": ""},
"7af2a874-4042-47e5-8065-4ed499901968": {"activated": 0, "id": "EAP_TTLS_CA_CERT", "type": ""},
"3e622df0-cbd4-4660-afdd-d7b813a2f130": {"activated": 0, "id": "MANOJ-5G", "type": ""},
"bd89b41c-5688-3e79-8353-f7391d8e5ce9": {"activated": 0, "id": "br-slave-wlan0", "type": ""},
"cb116408-6a95-42d0-add8-8b40bdd3b4cd": {"activated": 0, "id": "PEAP_GTC_CA_CERT", "type": ""},
"a99477b4-2ccd-42dc-909d-2c913851be2a": {"activated": 0, "id": "nagaveni 1", "type": ""},
"a33750a5-3623-49b4-b784-97d718acc959": {"activated": 0, "id": "find", "type": ""},
"54e7567b-505a-4d29-ba36-533bd2cf25dd": {"activated": 0, "id": "PEAP_WPA3_CA-cert", "type": ""},
"e5018996-4c3d-4caa-bd08-2dfc4424f520": {"activated": 0, "id": "wireless@telxsids.net", "type": ""},
"aa616959-ba9f-4ad4-bf5e-4eab629c5d58": {"activated": 1, "id": "internal-usb0"}
}
}
在这里,我需要通过引用id
来获取UUID的值。
例如:if(id == "PSK")
获取UUID: 8284b1b8-a268-4a99-88e4-fdf35f6badb0
。
请帮我解析嵌套的cJSON。
使用cJSON * deviceData = cJSON_GetObjectItem(root,"connections");
函数进行了尝试,但无法获取特定的字符串。
英文:
I need to extract key form nested Array JSON .
Nested C jOSN
{
"SDCERR": 0, "InfoMsg": "", "count": 18,
"connections": {
"b59519e0-917d-4c29-b31a-81e1837bd43e": {"activated": 0, "id": "PEAP_GTC_NO_CA_CERT", "type": ""},
"518b71d4-28ca-41c5-abb6-88245f0bf3fb": {"activated": 0, "id": "Manoj", "type": ""},
"91988884-d524-43a2-b255-88817330fd8b": {"activated": 0, "id": "NaveedP", "type": ""},
"26dfa985-cea3-4138-b03e-ab25f96e1dbb": {"activated": 0, "id": "PEAP_MSCHAPv2_NO_CA_CERT", "type": ""},
"0d82d7d3-a00a-45e3-a42b-66b13afc03f5": {"activated": 0, "id": "PEAP_MSCHAPv2_CA_CERT", "type": ""},
"1bb69f6c-d75e-44ad-a6ee-de7a5095b57e": {"activated": 0, "id": "EAP_TLS_NO_CA_CERT", "type": ""},
"566d673a-b48b-4e82-aaa5-724c61ed54c0": {"activated": 0, "id": "nagaveni", "type": ""},
"9f382a6d-8e3e-4a7e-88e3-33f0b885dcd0": {"activated": 0, "id": "EAP_TTLS_NO_CA_CERT", "type": ""},
"8284b1b8-a268-4a99-88e4-fdf35f6badb0": {"activated": 0, "id": "PSK", "type": ""},
"7af2a874-4042-47e5-8065-4ed499901968": {"activated": 0, "id": "EAP_TTLS_CA_CERT", "type": ""},
"3e622df0-cbd4-4660-afdd-d7b813a2f130": {"activated": 0, "id": "MANOJ-5G", "type": ""},
"bd89b41c-5688-3e79-8353-f7391d8e5ce9": {"activated": 0, "id": "br-slave-wlan0", "type": ""},
"cb116408-6a95-42d0-add8-8b40bdd3b4cd": {"activated": 0, "id": "PEAP_GTC_CA_CERT", "type": ""},
"a99477b4-2ccd-42dc-909d-2c913851be2a": {"activated": 0, "id": "nagaveni 1", "type": ""},
"a33750a5-3623-49b4-b784-97d718acc959": {"activated": 0, "id": "find", "type": ""},
"54e7567b-505a-4d29-ba36-533bd2cf25dd": {"activated": 0, "id": "PEAP_WPA3_CA-cert", "type": ""},
"e5018996-4c3d-4caa-bd08-2dfc4424f520": {"activated": 0, "id": "wireless@telxsids.net", "type": ""},
"aa616959-ba9f-4ad4-bf5e-4eab629c5d58": {"activated": 1, "id": "internal-usb0"}
}}
Here, by refering id
I need get the value of UUID
Example: if(id =="PSK")
get the UUID :8284b1b8-a268-4a99-88e4-fdf35f6badb0
.
Please help me how parse the nested cJSON.
Using cJSON * deviceData = cJSON_GetObjectItem(root,"connections");
function I tried, but unable to get the particular string.
答案1
得分: 0
使用cJSON_ArrayForEach(element, array)
宏来迭代数组或对象的元素并查找匹配项。对于对象,element
中的cJSON
指针的string
成员是当前元素的名称。
示例程序:
#include <cjson/cJSON.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
// 用于将JSON文件解析为cJSON结构的实用函数
cJSON *parse_json_file(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
return NULL;
}
struct stat st;
if (fstat(fd, &st) < 0) {
perror("fstat");
close(fd);
return NULL;
}
char *buffer = malloc(st.st_size);
if (!buffer) {
close(fd);
return NULL;
}
if (read(fd, buffer, st.st_size) != st.st_size) {
// 读取不完整,将其视为错误处理
perror("read");
close(fd);
free(buffer);
return NULL;
}
close(fd);
cJSON *cj = cJSON_ParseWithLength(buffer, st.st_size);
free(buffer);
return cj;
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s FILENAME ID\n", argv[0]);
return EXIT_FAILURE;
}
cJSON *cj = parse_json_file(argv[1]);
if (!cj) {
fprintf(stderr, "无法读取和解析 '%s'。\n", argv[1]);
return EXIT_FAILURE;
}
// 提取 .connections。注意:始终使用区分大小写版本的函数
cJSON *connections = cJSON_GetObjectItemCaseSensitive(cj, "connections");
if (!connections) {
fputs("JSON没有connections关键字。\n", stderr);
return EXIT_FAILURE;
}
if (!cJSON_IsObject(connections)) {
fputs("JSON connections值不是对象\n", stderr);
return EXIT_FAILURE;
}
// 迭代 .connections 对象的元素
const char *id = argv[2];
cJSON *conn;
cJSON_ArrayForEach(conn, connections) {
// 对于每个元素的值,查看它是否是具有匹配id字段的对象
if (cJSON_IsObject(conn)) {
cJSON *json_id = cJSON_GetObjectItemCaseSensitive(conn, "id");
if (json_id && cJSON_IsString(json_id) &&
strcmp(json_id->valuestring, id) == 0) {
// conn->string 是当前元素的键
printf("UUID: %s\n", conn->string);
}
}
}
cJSON_Delete(cj);
return 0;
}
用法:
$ gcc -g -O -Wall -Wextra example.c -lcjson
$ ./a.out input.json PSK
UUID: 8284b1b8-a268-4a99-88e4-fdf35f6badb0
英文:
Use the cJSON_ArrayForEach(element, array)
macro to iterate over the elements of an array or object and look for a match. For objects, the element
cJSON
pointer's string
member is the name of the current element.
Example program:
#include <cjson/cJSON.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
// Utility function to read a JSON file into a cJSON struct
cJSON *parse_json_file(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
return NULL;
}
struct stat st;
if (fstat(fd, &st) < 0) {
perror("fstat");
close(fd);
return NULL;
}
char *buffer = malloc(st.st_size);
if (!buffer) {
close(fd);
return NULL;
}
if (read(fd, buffer, st.st_size) != st.st_size) {
// Incomplete read; treat as an error
perror("read");
close(fd);
free(buffer);
return NULL;
}
close(fd);
cJSON *cj = cJSON_ParseWithLength(buffer, st.st_size);
free(buffer);
return cj;
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s FILENAME ID\n", argv[0]);
return EXIT_FAILURE;
}
cJSON *cj = parse_json_file(argv[1]);
if (!cj) {
fprintf(stderr, "Unable to read and parse '%s'.\n", argv[1]);
return EXIT_FAILURE;
}
// Extract .connections. Note: Always use the CaseSensitive versions of functions
cJSON *connections = cJSON_GetObjectItemCaseSensitive(cj, "connections");
if (!connections) {
fputs("JSON has no connections key.\n", stderr);
return EXIT_FAILURE;
}
if (!cJSON_IsObject(connections)) {
fputs("JSON connections value isn't an object\n", stderr);
return EXIT_FAILURE;
}
// Iterate over the elements of the .connections object
const char *id = argv[2];
cJSON *conn;
cJSON_ArrayForEach(conn, connections) {
// For each element's value, see if it's object with a matching id field
if (cJSON_IsObject(conn)) {
cJSON *json_id = cJSON_GetObjectItemCaseSensitive(conn, "id");
if (json_id && cJSON_IsString(json_id) &&
strcmp(json_id->valuestring, id) == 0) {
// conn->string is the key of the current element
printf("UUID: %s\n", conn->string);
}
}
}
cJSON_Delete(cj);
return 0;
}
and usage:
$ gcc -g -O -Wall -Wextra example.c -lcjson
$ ./a.out input.json PSK
UUID: 8284b1b8-a268-4a99-88e4-fdf35f6badb0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论