当前位置:网站首页>[cjson] cjson learning notes (II)

[cjson] cjson learning notes (II)

2021-09-15 04:52:49 itcast0

1. Important function description
    【1】 Two create
    【 establish JSON object 】cJSON *cJSON_CreateObject(void);
    【 establish JSON Array 】cJSON *cJSON_CreateArray(void);
    
    【2】 Two kinds of addition
    【 Add... To the object 】void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
    【 Add... To the array 】void cJSON_AddItemToArray(cJSON *array, cJSON *item);
 
    【3】 Common moves
    【 Add a number to the object 】cJSON_AddItemToObject(root, "value", cJSON_CreateNumber(value));
    【 Add a file to the object 】cJSON_AddItemToObject(root, "string", cJSON_CreateString(string));
 
    【4】JSON nesting
    【 Add an array to the object 】cJSON_AddItemToObject(root, "rows", rows = cJSON_CreateArray());
    【 Add objects to the array 】cJSON_AddItemToArray(rows, row = cJSON_CreateObject());
 
2. Create a variety of JSON Data packets
    Here are a few examples through the code , For more information, see the relevant files in the code warehouse .
    【1】JSON Numbers
[cpp]  view plain copy
 
  1. void create_single_number(void) {  
  2.     cJSON *root;  
  3.     char *out;  
  4.     int value = 24;  
  5.   
  6.     root = cJSON_CreateObject(); //  Create a root   
  7.   
  8.     cJSON_AddItemToObject(root, "value", cJSON_CreateNumber(value));  
  9.   
  10.     //  Print and release   
  11.     out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);  
  12.   
  13.     //  Console output   
  14. #if 0  
  15.     {  
  16.         "value": 24  
  17.     }  
  18. #endif  
  19. }  
    【 Briefly explain 】
    【1】cJSON_AddItemToObject(root, "value", cJSON_CreateNumber(value)); 
    【2】cJSON_AddNumberToObject(root, "value", value); 
    【1】 and 【2】 The effect is exactly the same .
 
    【2】JSON character string
[cpp]  view plain copy
 
  1. void create_single_string(void) {  
  2.     cJSON *root;  
  3.     char *out;  
  4.     char *name = "xukai871105";  
  5.   
  6.     root = cJSON_CreateObject(); //  Create a root   
  7.     //  Method   Use cJSON_AddItemToObject, Recommended   
  8.     cJSON_AddItemToObject(root, "name", cJSON_CreateString(name));  
  9.   
  10.     //  Print and release   
  11.     out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);  
  12.   
  13.     //  Console output   
  14. #if 0  
  15.     {  
  16.         "name": "xukai871105"  
  17.     }  
  18. #endif  
  19. }  
    【 Briefly explain 】
    【1】 cJSON_AddItemToObject(root, "name", cJSON_CreateString(name));
    【2】 cJSON_AddStringToObject(root, "name",name);
    【1】 and 【2】 The effect is exactly the same .
 
    【3】JSON Boolean type
[cpp]  view plain copy
 
  1. void create_bool(void) {  
  2.     cJSON *root;  
  3.     char *out;  
  4.   
  5.     root = cJSON_CreateObject(); //  Create a root   
  6.   
  7.     cJSON_AddItemToObject(root, "success", cJSON_CreateFalse());  
  8.   
  9.     //  Print and release   
  10.     out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);  
  11.   
  12.     //  Console output   
  13. #if 0  
  14.     {  
  15.         "success": false  
  16.     }  
  17. #endif  
  18. }  
    【 Briefly explain 】
    【1】 Boolean types don't need quotes .
 
3.JSON Format nesting
    JSON Formats are often nested when used , for example JSON Object JSON Array and JSON Nested in an array JSON object , Here are some simple examples to illustrate the problem .
    【1】JSON Simple nesting
[cpp]  view plain copy
 
  1. void create_simple(void) {  
  2.     cJSON *root;  
  3.     char *out;  
  4.     int list[4] = {5,6,7,8};  
  5.   
  6.     root = cJSON_CreateObject(); //  Create a root   
  7.     cJSON_AddItemToObject(root, "lists", cJSON_CreateIntArray(list, 4));  
  8.   
  9.     //  Print and release   
  10.     out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);  
  11.   
  12.     //  Console output   
  13. #if 0  
  14.     {  
  15.         "lists": [5, 6, 7, 8]  
  16.     }  
  17. #endif  
  18. }  
    【2】JSON Complex nesting
[cpp]  view plain copy
 
  1. void create_complex(void) {  
  2. cJSON *root, *rows, *row;  
  3. char *out;  
  4. int i = 0;  
  5.   
  6. char *title[3] = { " Raspberry pie study notes —— Index blog ",  
  7. " Raspberry pie study notes ——GPIO Functional learning ",  
  8. " Internet of things learning notes —— Index blog "};  
  9. char *url[3] = { "http://blog.csdn.net/xukai871105/article/details/23115627",  
  10. "http://blog.csdn.net/xukai871105/article/details/12684617",  
  11. "http://blog.csdn.net/xukai871105/article/details/23366187"};  
  12.   
  13. root = cJSON_CreateObject(); //  Create a root   
  14. cJSON_AddNumberToObject(root, "total", 3);  
  15.   
  16. //  stay object Add array  
  17. cJSON_AddItemToObject(root, "rows", rows = cJSON_CreateArray());  
  18.   
  19. for(i = 0; i < 3; i++) {  
  20. //  stay array Add object  
  21. cJSON_AddItemToArray(rows, row = cJSON_CreateObject());  
  22. cJSON_AddItemToObject(row, "title", cJSON_CreateString(title[i]));  
  23. cJSON_AddItemToObject(row, "url", cJSON_CreateString(url[i]));  
  24. }  
  25.   
  26. //  Print and release   
  27. out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);  
  28.   
  29. //  Console output   
  30. #if 0  
  31. {  
  32. "total": 3,  
  33. "rows": [{  
  34. "title": " Raspberry pie study notes —— Index blog ",  
  35. "url": "http://blog.csdn.net/xukai871105/article/details/23115627"  
  36. }, {  
  37. "title": " Raspberry pie study notes ——GPIO Functional learning ",  
  38. "url": "http://blog.csdn.net/xukai871105/article/details/12684617"  
  39. }, {  
  40. "title": " Internet of things learning notes —— Index blog ",  
  41. "url": "http://blog.csdn.net/xukai871105/article/details/23366187"  
  42. }]  
  43. }  
  44. #endif  
  45. }  
    【 Briefly explain 】
rows by JSON object ,rows Object JSON Array , every last JSON The element of the array is another JSON object , The object name is row,row Object has two key value pairs , Namely titile and url.
 
2015/08/19 17:50
 
【 Other reference examples 】
 
1. Use of functions
 
Macro definition prototype :
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name)     cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b)    cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n)  cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s)  cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
 
Example :
    cJSON *root = cJSON_CreateObject();   // Create an object first
    cJSON_AddNumberToObject(root, "value", 123.4);
    cJSON_AddStringToObject(root, "year", "2015");
    cJSON_AddNullToObject(root, "secret");
    cJSON_AddTrueToObject(root, "Bool");
    cJSON_AddFalseToObject(root, "false");
    cJSON_AddBoolToObject(root, "Yes", 3);
    cJSON_AddBoolToObject(root, "Yes", 0);
    out = cJSON_Print(root);     // Print object
    //out = cJSON_PrintUnformatted(root);   // Unformatted printing
    //printf("===> 2: %s\n", out);
    printf("%s\n", out);
    cJSON_Delete(root);   // Free the memory occupied by the object
    free(out);
 
Prototypes of functions :
/* These calls create a cJSON item of the appropriate type. */
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
 
Example : Refer to the code of the blog above
 
The function prototype :
/* These utilities create an Array of count items. */
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
 
/* Append item to the specified array/object. */
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
extern void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item); 
 
Example :
    char *out = NULL;  
    int array[] = {1, 2, 3, 4};
    int array1[] = {1, 2, 3, 4, 5, 6};
    cJSON *json = NULL;
    cJSON *item = NULL;
    cJSON *tmp = NULL;
 
    json  = cJSON_CreateObject();
    //cJSON_AddItemToObject(json,"array",  cJSON_CreateIntArray(array, 4));   // Equivalent to the following two lines
    item = cJSON_CreateIntArray(array, 4);
    cJSON_AddItemToObject(json, "array", item);
    out = cJSON_Print(json);
    printf("%s\n", out);
 
    printf("=========================\n");
    printf("size: %d\n", cJSON_GetArraySize(item));   // Output is 4  4 Elements
 
    tmp = cJSON_GetArrayItem(json, 0);   // obtain json The first node in
    if (NULL != tmp)
    {  
        printf("ok..\n");  
        printf("size: %d\n", cJSON_GetArraySize(tmp));
    }  
    else
        printf("error...\n");
 
    // here item The elements in the array have  1 2 3 4 5 6 7 
    printf("=========================\n");
    cJSON_AddItemToArray(item, cJSON_CreateNumber(5));
    cJSON_AddItemToArray(item, cJSON_CreateNumber(6));
    cJSON_AddItemToArray(item, cJSON_CreateNumber(7));
    printf("size: %d\n", cJSON_GetArraySize(item));
    out = cJSON_Print(json);
    printf("%s\n", out);
 
    tmp = cJSON_CreateIntArray(array1, 6);
    //cJSON_AddItemToObject(json, "array1", tmp);   // It seems to be equivalent to the following sentence
    cJSON_AddItemReferenceToObject(json, "array1", tmp);
    out = cJSON_Print(json);
    printf("%s\n", out);
 
 
    // Reference example of shaping array
    int array3[] = {1, 3, 5, 7, 9};
    json = cJSON_CreateIntArray(array3, 5);
    out = cJSON_Print(json);    cJSON_Delete(json);    printf("%s\n", out);   free(out); 
 
    // Character pointer array
    const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    json = cJSON_CreateStringArray(strings, 7);  
    out = cJSON_Print(json);    cJSON_Delete(json);   printf("%s\n", out);   free(out); 
 
 

版权声明
本文为[itcast0]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/09/20210909114147596c.html

随机推荐