json简单实例

本文介绍了C语言中json库的使用,包括创建和操作JSON对象,如创建对象、获取值、添加对象域等。此外,还讲解了JSON的两种基本格式:对象和数组,并提供了相关API接口函数的示例。
摘要由CSDN通过智能技术生成

                                                                                                            json

 

      json 格式有两种                          

                       1:   键  --  值 的集合        抽象为对象 object 

 

                                                                        例如:{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" } 

   

                       2:   有序的列表               抽象为数组 array

 

                                                                        例如:

 

                                                                    { "people": [

 

                                                                                  { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

 

                                                                                  { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},

 

                                                                                  { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

 

                                                                                            ]

                                                                              }

                                                                                              注意数组元素之间用  “逗号”  隔开,且最后一个元素无  “逗号”

     

 

     

 

     json_type_object    --值 对的集合

  

 

                 json对象值的类型:

                

                                      json_type_boolean

                                      json_type_double, 

                                      json_type_int, 

                                      json_type_array, “值”的集合

                                      json_type_string

 

 

     API接口函数:

 

                      struct json_object *json_object_new_object();

  

                                         创建一个 struct json_object 类型的json对象

 

                      struct json_object* json_object_new_boolean(boolean b);

 

                                         创建一个 struct json_boolean值 类型的json对象

 

                      boolean json_object_get_boolean(struct json_object *obj);

 

                                         从json对象中boolean值类型得到boolean

 

 

 

                        说明:同上向类似

                         struct json_object* json_object_new_int(int i) 

 

                         int json_object_get_int(struct json_object *this) 

 

                         struct json_object* json_object_new_double(double d) 

 

                         double json_object_get_double(struct json_object *this) 

 

                         struct json_object* json_object_new_string(char *s) 

 

                         char* json_object_get_string(struct json_object *this)  

 

 

       数组值类型的集合: 

 

                   

                     struct json_object *json_object_new_array();

       

                                        创建个空的json_type_array类型JSON数组值对象

 

                     struct json_object *json_tokener_parse(char *str);                                  

 

                                        由str里的json字符串生成json对象,strson_object_to_json_string()生成的

                                   参数:str     -----    json字符串

 

                     struct json_object *json_object_object_get(struct json_object *json,char *name);

 

                                        从json中按名字取一个对象

                                   参数:json    -----    json对象

                                    name    -----    json域名字

 

                     struct json_object * * json_object_get(struct json_object * this)  

                                       

                                   参数: this   -----    json对象    

            

                     Void json_object_put(struct json_object * this)

 

                                   释放对象引用次数一次

 

                        例子:

 

                                      ---------------------------------------------------------------------------------------

                                  my_string = json_object_new_string("\t"); 

                                  printf("my_string=%s\n", json_object_get_string(my_string));                              

                                  printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));

                                  json_object_put(my_string);

                                      ---------------------------------------------------------------------------------------

 

                    Int json_object_is_type(struct json_object * this, enum json_type type)

 

                                      检查json_objectjson的某个类型

 

                                 参数:this   ----- json_object 实例

                                  type   ----- json_type_boolean,

                                               json_type_double,

                                               json_type_int,

                                               json_type_object, 

                                               json_type_array,

                                               json_type_string

 

                   enum json_type json_object_get_type(struct json_object * this )

 

                                       得到json_object的类型  

 

                                 参数: this   ----- json_object 实例       

                                       

 

                   char * json_object_to_json_string(struct json_object * this)

 

                                       将json_object内容转换json格式字符串,其中可能含有转义符

                                 参数: this   ----- json_object 实例 

                                返回值:json格式字符串

 

                   void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);

                                

                                       添加个对象域到json对象中

                                 参数:Obj —— json对象

                                  key —— 域名字

                                  val —— json值对象

 

                  void json_object_object_del(struct json_object* obj, char *key);

                                       删除keyjson对象

                                 参数:同上

 

                  int json_object_array_length(struct json_object *obj);

 

                                       获得json对象数组的长度

                                 参数:obj —— json数组值对象

 

                  extern int json_object_array_add(struct json_object *obj,struct json_object *val);

 

                                       添加一元素在json数组对象末端

                                 参数:obj —— json数组值对象

                                  val —— json值对象

 

                  int json_object_array_put_idx(struct json_object *obj, int idx,struct json_object *val);

                                       在指定的json数组对象下标插入或替换一个json对象元素

                                 参数:obj —— json数组值对象

                                  val —— json值对象

                                  idx —— 数组下标

 

                  struct json_object * json_object_array_get_idx(struct json_object * json_array,int i

 

                                       从数组中按下标取json值对象

                                 参数:json_array ---- 数组值对象

                                  i   ---- 数组下标位置

                    定义宏   json_object_object_foreach(obj,key,val)

 

                                 参数: 遍历json对象的key和值

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值