从嵌套数组JSON中提取关键内容使用cJSON

huangapple go评论86阅读模式
英文:

Extracting Key from nested array JSON using cJSON

问题

我需要从嵌套的数组JSON中提取关键信息。
嵌套的C JSON

  1. {
  2. "SDCERR": 0, "InfoMsg": "", "count": 18,
  3. "connections": {
  4. "b59519e0-917d-4c29-b31a-81e1837bd43e": {"activated": 0, "id": "PEAP_GTC_NO_CA_CERT", "type": ""},
  5. "518b71d4-28ca-41c5-abb6-88245f0bf3fb": {"activated": 0, "id": "Manoj", "type": ""},
  6. "91988884-d524-43a2-b255-88817330fd8b": {"activated": 0, "id": "NaveedP", "type": ""},
  7. "26dfa985-cea3-4138-b03e-ab25f96e1dbb": {"activated": 0, "id": "PEAP_MSCHAPv2_NO_CA_CERT", "type": ""},
  8. "0d82d7d3-a00a-45e3-a42b-66b13afc03f5": {"activated": 0, "id": "PEAP_MSCHAPv2_CA_CERT", "type": ""},
  9. "1bb69f6c-d75e-44ad-a6ee-de7a5095b57e": {"activated": 0, "id": "EAP_TLS_NO_CA_CERT", "type": ""},
  10. "566d673a-b48b-4e82-aaa5-724c61ed54c0": {"activated": 0, "id": "nagaveni", "type": ""},
  11. "9f382a6d-8e3e-4a7e-88e3-33f0b885dcd0": {"activated": 0, "id": "EAP_TTLS_NO_CA_CERT", "type": ""},
  12. "8284b1b8-a268-4a99-88e4-fdf35f6badb0": {"activated": 0, "id": "PSK", "type": ""},
  13. "7af2a874-4042-47e5-8065-4ed499901968": {"activated": 0, "id": "EAP_TTLS_CA_CERT", "type": ""},
  14. "3e622df0-cbd4-4660-afdd-d7b813a2f130": {"activated": 0, "id": "MANOJ-5G", "type": ""},
  15. "bd89b41c-5688-3e79-8353-f7391d8e5ce9": {"activated": 0, "id": "br-slave-wlan0", "type": ""},
  16. "cb116408-6a95-42d0-add8-8b40bdd3b4cd": {"activated": 0, "id": "PEAP_GTC_CA_CERT", "type": ""},
  17. "a99477b4-2ccd-42dc-909d-2c913851be2a": {"activated": 0, "id": "nagaveni 1", "type": ""},
  18. "a33750a5-3623-49b4-b784-97d718acc959": {"activated": 0, "id": "find", "type": ""},
  19. "54e7567b-505a-4d29-ba36-533bd2cf25dd": {"activated": 0, "id": "PEAP_WPA3_CA-cert", "type": ""},
  20. "e5018996-4c3d-4caa-bd08-2dfc4424f520": {"activated": 0, "id": "wireless@telxsids.net", "type": ""},
  21. "aa616959-ba9f-4ad4-bf5e-4eab629c5d58": {"activated": 1, "id": "internal-usb0"}
  22. }
  23. }

在这里,我需要通过引用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

  1. {
  2. "SDCERR": 0, "InfoMsg": "", "count": 18,
  3. "connections": {
  4. "b59519e0-917d-4c29-b31a-81e1837bd43e": {"activated": 0, "id": "PEAP_GTC_NO_CA_CERT", "type": ""},
  5. "518b71d4-28ca-41c5-abb6-88245f0bf3fb": {"activated": 0, "id": "Manoj", "type": ""},
  6. "91988884-d524-43a2-b255-88817330fd8b": {"activated": 0, "id": "NaveedP", "type": ""},
  7. "26dfa985-cea3-4138-b03e-ab25f96e1dbb": {"activated": 0, "id": "PEAP_MSCHAPv2_NO_CA_CERT", "type": ""},
  8. "0d82d7d3-a00a-45e3-a42b-66b13afc03f5": {"activated": 0, "id": "PEAP_MSCHAPv2_CA_CERT", "type": ""},
  9. "1bb69f6c-d75e-44ad-a6ee-de7a5095b57e": {"activated": 0, "id": "EAP_TLS_NO_CA_CERT", "type": ""},
  10. "566d673a-b48b-4e82-aaa5-724c61ed54c0": {"activated": 0, "id": "nagaveni", "type": ""},
  11. "9f382a6d-8e3e-4a7e-88e3-33f0b885dcd0": {"activated": 0, "id": "EAP_TTLS_NO_CA_CERT", "type": ""},
  12. "8284b1b8-a268-4a99-88e4-fdf35f6badb0": {"activated": 0, "id": "PSK", "type": ""},
  13. "7af2a874-4042-47e5-8065-4ed499901968": {"activated": 0, "id": "EAP_TTLS_CA_CERT", "type": ""},
  14. "3e622df0-cbd4-4660-afdd-d7b813a2f130": {"activated": 0, "id": "MANOJ-5G", "type": ""},
  15. "bd89b41c-5688-3e79-8353-f7391d8e5ce9": {"activated": 0, "id": "br-slave-wlan0", "type": ""},
  16. "cb116408-6a95-42d0-add8-8b40bdd3b4cd": {"activated": 0, "id": "PEAP_GTC_CA_CERT", "type": ""},
  17. "a99477b4-2ccd-42dc-909d-2c913851be2a": {"activated": 0, "id": "nagaveni 1", "type": ""},
  18. "a33750a5-3623-49b4-b784-97d718acc959": {"activated": 0, "id": "find", "type": ""},
  19. "54e7567b-505a-4d29-ba36-533bd2cf25dd": {"activated": 0, "id": "PEAP_WPA3_CA-cert", "type": ""},
  20. "e5018996-4c3d-4caa-bd08-2dfc4424f520": {"activated": 0, "id": "wireless@telxsids.net", "type": ""},
  21. "aa616959-ba9f-4ad4-bf5e-4eab629c5d58": {"activated": 1, "id": "internal-usb0"}
  22. }}

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成员是当前元素的名称。

示例程序:

  1. #include <cjson/cJSON.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. // 用于将JSON文件解析为cJSON结构的实用函数
  9. cJSON *parse_json_file(const char *filename) {
  10. int fd = open(filename, O_RDONLY);
  11. if (fd < 0) {
  12. return NULL;
  13. }
  14. struct stat st;
  15. if (fstat(fd, &st) < 0) {
  16. perror("fstat");
  17. close(fd);
  18. return NULL;
  19. }
  20. char *buffer = malloc(st.st_size);
  21. if (!buffer) {
  22. close(fd);
  23. return NULL;
  24. }
  25. if (read(fd, buffer, st.st_size) != st.st_size) {
  26. // 读取不完整,将其视为错误处理
  27. perror("read");
  28. close(fd);
  29. free(buffer);
  30. return NULL;
  31. }
  32. close(fd);
  33. cJSON *cj = cJSON_ParseWithLength(buffer, st.st_size);
  34. free(buffer);
  35. return cj;
  36. }
  37. int main(int argc, char **argv) {
  38. if (argc != 3) {
  39. fprintf(stderr, "Usage: %s FILENAME ID\n", argv[0]);
  40. return EXIT_FAILURE;
  41. }
  42. cJSON *cj = parse_json_file(argv[1]);
  43. if (!cj) {
  44. fprintf(stderr, "无法读取和解析 '%s'。\n", argv[1]);
  45. return EXIT_FAILURE;
  46. }
  47. // 提取 .connections。注意:始终使用区分大小写版本的函数
  48. cJSON *connections = cJSON_GetObjectItemCaseSensitive(cj, "connections");
  49. if (!connections) {
  50. fputs("JSON没有connections关键字。\n", stderr);
  51. return EXIT_FAILURE;
  52. }
  53. if (!cJSON_IsObject(connections)) {
  54. fputs("JSON connections值不是对象\n", stderr);
  55. return EXIT_FAILURE;
  56. }
  57. // 迭代 .connections 对象的元素
  58. const char *id = argv[2];
  59. cJSON *conn;
  60. cJSON_ArrayForEach(conn, connections) {
  61. // 对于每个元素的值,查看它是否是具有匹配id字段的对象
  62. if (cJSON_IsObject(conn)) {
  63. cJSON *json_id = cJSON_GetObjectItemCaseSensitive(conn, "id");
  64. if (json_id && cJSON_IsString(json_id) &&
  65. strcmp(json_id->valuestring, id) == 0) {
  66. // conn->string 是当前元素的键
  67. printf("UUID: %s\n", conn->string);
  68. }
  69. }
  70. }
  71. cJSON_Delete(cj);
  72. return 0;
  73. }

用法:

  1. $ gcc -g -O -Wall -Wextra example.c -lcjson
  2. $ ./a.out input.json PSK
  3. 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:

  1. #include &lt;cjson/cJSON.h&gt;
  2. #include &lt;fcntl.h&gt;
  3. #include &lt;stdio.h&gt;
  4. #include &lt;stdlib.h&gt;
  5. #include &lt;string.h&gt;
  6. #include &lt;sys/stat.h&gt;
  7. #include &lt;unistd.h&gt;
  8. // Utility function to read a JSON file into a cJSON struct
  9. cJSON *parse_json_file(const char *filename) {
  10. int fd = open(filename, O_RDONLY);
  11. if (fd &lt; 0) {
  12. return NULL;
  13. }
  14. struct stat st;
  15. if (fstat(fd, &amp;st) &lt; 0) {
  16. perror(&quot;fstat&quot;);
  17. close(fd);
  18. return NULL;
  19. }
  20. char *buffer = malloc(st.st_size);
  21. if (!buffer) {
  22. close(fd);
  23. return NULL;
  24. }
  25. if (read(fd, buffer, st.st_size) != st.st_size) {
  26. // Incomplete read; treat as an error
  27. perror(&quot;read&quot;);
  28. close(fd);
  29. free(buffer);
  30. return NULL;
  31. }
  32. close(fd);
  33. cJSON *cj = cJSON_ParseWithLength(buffer, st.st_size);
  34. free(buffer);
  35. return cj;
  36. }
  37. int main(int argc, char **argv) {
  38. if (argc != 3) {
  39. fprintf(stderr, &quot;Usage: %s FILENAME ID\n&quot;, argv[0]);
  40. return EXIT_FAILURE;
  41. }
  42. cJSON *cj = parse_json_file(argv[1]);
  43. if (!cj) {
  44. fprintf(stderr, &quot;Unable to read and parse &#39;%s&#39;.\n&quot;, argv[1]);
  45. return EXIT_FAILURE;
  46. }
  47. // Extract .connections. Note: Always use the CaseSensitive versions of functions
  48. cJSON *connections = cJSON_GetObjectItemCaseSensitive(cj, &quot;connections&quot;);
  49. if (!connections) {
  50. fputs(&quot;JSON has no connections key.\n&quot;, stderr);
  51. return EXIT_FAILURE;
  52. }
  53. if (!cJSON_IsObject(connections)) {
  54. fputs(&quot;JSON connections value isn&#39;t an object\n&quot;, stderr);
  55. return EXIT_FAILURE;
  56. }
  57. // Iterate over the elements of the .connections object
  58. const char *id = argv[2];
  59. cJSON *conn;
  60. cJSON_ArrayForEach(conn, connections) {
  61. // For each element&#39;s value, see if it&#39;s object with a matching id field
  62. if (cJSON_IsObject(conn)) {
  63. cJSON *json_id = cJSON_GetObjectItemCaseSensitive(conn, &quot;id&quot;);
  64. if (json_id &amp;&amp; cJSON_IsString(json_id) &amp;&amp;
  65. strcmp(json_id-&gt;valuestring, id) == 0) {
  66. // conn-&gt;string is the key of the current element
  67. printf(&quot;UUID: %s\n&quot;, conn-&gt;string);
  68. }
  69. }
  70. }
  71. cJSON_Delete(cj);
  72. return 0;
  73. }

and usage:

  1. $ gcc -g -O -Wall -Wextra example.c -lcjson
  2. $ ./a.out input.json PSK
  3. UUID: 8284b1b8-a268-4a99-88e4-fdf35f6badb0

huangapple
  • 本文由 发表于 2023年6月9日 12:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定