jsonc库使用
来源:互联网 发布:淘宝色差回复 编辑:程序博客网 时间:2024/05/17 09:11
jsonc库使用
作者:wangkangluo1 | 出处:博客园 | 2011/12/15 15:12:28 | 阅读58次
原文地址:
1: 下载json-c库源码文件
json-c-0.9.tar.gz
2: 编译
3: json-c常用函数
(1): 将一个json文件转换成object对象:
struct json_object* json_object_from_file(char *filename)
举例:
json_object *pobj = NULL;
pobj = json_object_from_file("/home/boy/tmp/test/jsontest/test.json");
(2): 将json-object写回文件
int json_object_to_file(char *filename, struct json_object *obj)
举例:
json_object_from_file("/home/boy/tmp/test/jsontest/test.json", pobj);
(3): 删除一个对象
void json_object_object_del(struct json_object* jso, const char *key);
(4): 增加一个对象
void json_object_object_add(struct json_object* jso, const char *key, struct json_object *val);
举例:
json_object_object_add(pobj, "Name", json_object_new_string("Andy"));
json_object_object_add(pobj, "Age", json_object_new_int(200));
(5): 释放对象
void json_object_put(struct json_object *jso);
4: 使用举例
(1): 新建一个x.json文件
{
"item1": "I love JSON",
"foo": "You love Json",
"item3": "We love JSON",
"Name": "Andy",
"Age": 28
}
(2): 程序
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "json.h"
void GetValByKey(json_object * jobj, const char *sname)
{
json_object *pval = NULL;
enum json_type type;
pval = json_object_object_get(jobj, sname);
if(NULL!=pval){
type = json_object_get_type(pval);
switch(type)
{
case json_type_string:
printf("Key:%s value: %s\n", sname, json_object_get_string(pval));
break;
case json_type_int:
printf("Key:%s value: %d\n", sname, json_object_get_int(pval));
break;
default:
break;
}
}
}
int main(void)
{
json_object *pobj = NULL;
//pobj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
//printf("new_obj.to_string()=%s\n", json_object_to_json_string(pobj));
//json_parse(pobj);
pobj = json_object_from_file("/home/boy/tmp/test/jsontest/test.json");
GetValByKey(pobj, "foo");
json_object_object_del(pobj, "foo");
json_object_object_add(pobj, "foo", json_object_new_string("fark"));
json_object_object_add(pobj, "Age", json_object_new_int(200));
GetValByKey(pobj, "Age");
json_object_to_file("/home/boy/tmp/test/jsontest/new.json", pobj);
json_object_put(pobj);
return 0;
}
Makefile
all:server
server:
gcc -std=c99 -msse2 -Wall -O3 -o main main.c -ljson -lcrypto -lm -lpthread
完
- jsonc库使用
- jsonc库使用
- jsonc库使用
- jsonc库使用
- jsonc库使用
- jsonc库使用
- jsonc库使用
- jsonc库的安装以及简单使用
- jsonc的基本使用
- 利用JSONC库遍历求出JSON数组的元素值
- jsonc demo
- jsonc 性能
- 关于Ubutun下的jsonc的安装
- JS 循环遍历JSON数据 分类: JS技术 JS JQuery 2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options":"[{
- libevent库的使用--定时器的使用
- 使用Automake 创建和使用静态库
- 使用和生成库
- tinyxml库的使用
- MVC设计模式个人简单介绍
- 如果用户是一只猴子 isUserAMonkey() --google真幽默
- SQL 和 MDX 比较
- 付款凭证列印
- 设计模式之观察者模式
- jsonc库使用
- SVN trunk, branches and tags
- 了解负载均衡 会话保持 session同步
- PHP输出CSV和EXCEL两种简单的方法
- bash shell (3) linux trap 命令
- 八款开源 Android 游戏引擎 (巨好的资源)
- 大数加法程序,仅供参考(陈渊)
- handler 使用 重写 handleMessage 消息队列
- Android各分辨率机器怎样使用同一版本apk