前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android下json-c库使用

Android下json-c库使用

作者头像
李小白是一只喵
发布2020-04-24 09:27:38
1.8K0
发布2020-04-24 09:27:38
举报
文章被收录于专栏:算法微时光算法微时光

json-c库

json-c 库中是在嵌入式开发中常用的库。

因为很多地方都以json数据数据交互协议, 尤其嵌入式web数据交互时通常会用到json格式, 因此如果需要在产品端进行json数据解析 , json-c 是一个比较不错的选择。

API接口

json-c库中有一个json对象 :jsonobj.它会将一个json文件解析为一个json对象.

使用方式: 1.解析json文件,获取一个解析后的json对象. 2.访问对应的key值. 3.使用后,释放json对象.

解析json文件

方式一:将json格式的字符串转成json对象

代码语言:javascript
复制
json_object* json_tokener_parse(const char *str);

方式二:具有json格式文本内容的文本文件转化为json对象

代码语言:javascript
复制
json_object* json_object_from_file(const char *filename);
访问对应的key值
代码语言:javascript
复制
 extern json_bool json_object_object_get_ex(struct json_object* obj,const char *key,struct json_object **value);
释放json文件
代码语言:javascript
复制
Void json_object_put(struct json_object * this);

其他常用API

(1)创建一个空的json_type_object类型JSON对象:

代码语言:javascript
复制
struct json_object * json_object_new_object(); 

(2)创建一个空的json_type_array类型JSON数组值对象:

代码语言:javascript
复制
struct json_object * json_object_new_array();

(3)从json中按名字取一个对象:

代码语言:javascript
复制
struct json_object * json_object_object_get(struct json_object * json,char *name);

(4)减少对象引用次数一次,当减少到0就释放(free)资源:

代码语言:javascript
复制
Void json_object_put(struct json_object * this);

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

代码语言:javascript
复制
char * json_object_to_json_string(struct json_object * this);

(6)添加一个对象域到json对象中:

代码语言:javascript
复制
void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);

(7)删除key值json对象:

代码语言:javascript
复制
void json_object_object_del(struct json_object* obj, char *key);

(8)得到json对象数组的长度:

代码语言:javascript
复制
int json_object_array_length(struct json_object *obj);

(9)添加一元素在json对象数组末端:

代码语言:javascript
复制
extern int json_object_array_add(struct json_object *obj, struct json_object *val);

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

代码语言:javascript
复制
int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);

(11)从数组中,按下标取JSON值对象:

代码语言:javascript
复制
struct json_object * json_object_array_get_idx(struct json_object * json_array,int i);

(12)得到json_object的类型:

代码语言:javascript
复制
enum json_type json_object_get_type(struct json_object * this ) 

参考代码[转载]

代码语言:javascript
复制
#include <stdio.h>
#include <json-c/json.h>
 
/*
{
  Name: haha,
  Id:   101,
  Age:  21,
  info:{
    number:  1,
    score:  91,
    type:    2,
    params: ""
  }
}
*/
 
// gcc json-c_parse_string.c -ljson-c
int main(int argc, char const *argv[])
{
  /* Declaring the json data's in json format. */
  char buf[] = "{ \"Name\": \"haha\", \"Id\": 101, \"Age\": 21, \"info\": { \"number\": 1, \"score\": 91, \"type\": 2, \"params\": \"w\" } }";
 
  /* Declaring the json_object. To pass the Json string to the newly created json_object. */
  json_object *new_obj = json_tokener_parse(buf);
  if (!new_obj)
    return -1;
 
  json_object *val_obj = NULL;
  json_object *result = NULL;
  const char *str = NULL;
 
  /* To get the data's then we have to get to the specific node by using the below function. */
 
  if( json_object_object_get_ex(new_obj, "Name", &val_obj) ) {
    str = json_object_get_string(val_obj);
    printf("Name: %s\n", str);
  }
  if( json_object_object_get_ex(new_obj, "Id", &val_obj) ) {
    str = json_object_get_string(val_obj);
    printf("Id: %s\n", str);
  }
  if( json_object_object_get_ex(new_obj, "Age", &val_obj) ) {
    str = json_object_get_string(val_obj);
    printf("Age: %s\n", str);
  }
 
  if( json_object_object_get_ex(new_obj, "info", &val_obj) ) {
    if( json_object_object_get_ex(val_obj, "number", &result) ) {
      printf("\tinfo -> number: %d\n", json_object_get_int(result));
    }
    if( json_object_object_get_ex(val_obj, "score", &result) ) {
      printf("\tinfo -> score: %d\n", json_object_get_int(result));
    }
    if( json_object_object_get_ex(val_obj, "type", &result) ) {
      printf("\tinfo -> type: %d\n", json_object_get_int(result));
    }
    if( json_object_object_get_ex(val_obj, "params", &result) ) {
      printf("\tinfo -> params: %s\n", json_object_get_string(result));
    }
  }
 
  json_object_put(new_obj); // to return the pointer to its originalobjects
 
  return 0;
}

参考

使用json-c 体会 LINUX下C语言操作JSON数据 JSON C语言API整理

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • json-c库
  • API接口
    • 解析json文件
      • 访问对应的key值
        • 释放json文件
        • 其他常用API
        • 参考代码[转载]
        • 参考
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档