Jansson库的使用简介

一、Jansson的安装:

二、jansson相关的API:

https://jansson.readthedocs.io/en/latest/apiref.html#c.json_t

string

object

array

error

number

三、jansson实际如何处理JSON文件:

 1 #include 
 2 #include "jansson.h"
 3 #include <string.h>
 4 #include 
 5 #include 
 6 
 7 using namespace std;
 8 
 9 int main() {
10     json_t val;
11     json_type eval = JSON_REAL;
12     bool Judge = 1;
13     int type = json_typeof(&val);
14     Judge = json_is_object(&val);
15     cout << "json_is_object:" << Judge << endl;
16     Judge = json_is_array(&val);
17     cout << "Json_is_array:" << Judge << endl;
18     cout << "The type of val:" << type << endl;
19 
20     json_t *array, *integer;
21     array = json_array(); // Create the json array
22     integer = json_integer(42); // Create the json integer
23     json_array_append(array,integer);
24     json_decref(integer);
25 
26     /* JSON-STRING */
27     json_auto_t *value = NULL;
28     value = json_string("Hello");
29     json_string_set(value, "World!");
30     size_t len = json_string_length(value);
31     cout << "The len of value:" << len << endl;
32 
33     /* JSON-NUMBER */
34     json_int_t number;
35     number = 100;
36     printf("number is %" JSON_INTEGER_FORMAT "\n",number);
37     number = json_integer_value(integer);
38     printf("value of integer(json_t) is %" JSON_INTEGER_FORMAT "\n",number);
39     integer = json_real(3.1415926);
40     double res = json_real_value(integer);
41     printf("The res is:%f\n",res);
42 
43     /* JSON-ARRAY */
44     json_t *Matrix = json_array();
45     json_array_append(Matrix,integer);
46     size_t Matrix_Size = json_array_size(Matrix);
47     printf("The size of Matrix:%d\n",Matrix_Size);
48 
49     /* JSON-OBJECT */
50     json_t *obj = json_object();
51     size_t obj_size = json_object_size(obj);
52     printf("The size of obj:%d\n",obj_size);
53     char *key = "Hello";
54     int back = json_object_set(obj,key,&val);
55     printf("The json_object_set correct?%d\n",back);
56 
57     json_t *json_file;
58     json_error_t error;
59 
60     /* Use the abs-route of the file can read the json file crectlly */
61     json_file = json_load_file("/home/ubuntu-bitmain/workspace/TestCpp/src/cpuminer-conf.json", 0, &error);
62     if(!json_file) printf("No json_file can read!\n");
63     obj_size = json_object_size(json_file);
64     printf("The size of json_file:%d\n",obj_size);
65     json_t *url,*user,*pass;
66     char *Key_0 = "url";
67     char *Key_1 = "user";
68     char *Key_2 = "pass";
69     url = json_object_get(json_file,Key_0);
70     user = json_object_get(json_file,Key_1);
71     pass = json_object_get(json_file,Key_2);
72 
73     Judge = json_is_string(url);
74     size_t str_length = json_string_length(url);
75     cout << "The length of url is:" << str_length << endl;
76     const char *str_res_0 = " ";
77     str_res_0 = json_string_value(url);
78     cout << "The length of url is:" << strlen(str_res_0) << endl;
79     for(int i=0;i){
80         cout << *(str_res_0+i);
81     }
82     cout << endl;
83     cout << "The url is string" << Judge << endl;
84 
85     return 0;
86 }

 

四、从网上获取JSON文件并解析:

完~

 

转载于:https://www.cnblogs.com/uestc-mm/p/10793703.html

你可能感兴趣的:(Jansson库的使用简介)