json常用的一些知识

来源:互联网 发布:安卓新闻推荐 知乎 编辑:程序博客网 时间:2024/05/21 17:18
          json常用的一些方法
将各一些成员数值生成一个json对像,简单代码例子如下:
 struct json_object *fat,*json_sno,*json_name,*json_score;
 int sno = 1001;
    char name[] = "zhangsan";
    float score = 97.8;

    //json打包
    //创建一个Json对象
    fat = json_object_new_object();
    
    //将不同类型数据转换成json对应的类型
    json_sno = json_object_new_int(sno);
    json_name= json_object_new_string(name);
    json_score = json_object_new_double(score);

    //将json类型的数据加入到json对象中
    json_object_object_add(fat,"sno",json_sno);
    json_object_object_add(fat,"name",json_name);
    json_object_object_add(fat,"score",json_score);

    //将JSON对象转换成字符串
    const char * json_string = json_object_to_json_string(fat);
    printf("%s\n",json_string);

反过来,将json对象的字符串分解成一个个成员的值:


   char *recv_string = "{ \"sno\": 1001, \"name\": \"zhangsan\", \"score\": 97.800003 }";
    printf("%s\n",recv_string);
    
    struct json_object *object,*fat,*json_sno,*json_name,*json_score;
    int sno;
    char *name;
    float  score;
    int num;
    
     fat = json_tokener_parse(recv_string);
    //fat = json_object_array_get_idex(object,0);
    //fat = json_object_from_file(recv_string);
    json_sno = json_object_object_get(fat,"sno");
    json_name= json_object_object_get(fat,"name");
    json_score = json_object_object_get(fat,"score");
    
    sno = json_object_get_int(json_sno);
    name = (char*)json_object_get_string(json_name);
    score = (float)json_object_get_double(json_score);
    
    printf("%d\t%s\t%f\n",sno,name,score);

将数组打包成json对象:

例子:
void json_to_string(int a[], int n){
    struct json_object *my_array,*obj;
    my_array = json_object_new_array();
    int i ,m;

    for(i = 0; i < 5;i ++){
        json_object_array_add(my_array, json_object_new_int(a[i]));
    }
    printf("my_array=\n");  
    for(i=0; i < json_object_array_length(my_array); i++) {
        obj = json_object_array_get_idx(my_array, i);
        m = json_object_get_int(obj);
        printf("%d\n",m);
        //      printf("\ta[%d]=%s \n", i, json_object_to_json_string(obj));
    }
    printf("my_array.to_string()=%s \n", json_object_to_json_string(my_array));
}


反过来,将json数组的对象分解出来:

void string_to_array(char *arg)
{
     int sno,i;
     struct json_object *fat, *obj;
     char *recv_string = arg;
    printf("%s\n",recv_string);

    fat = json_tokener_parse(recv_string);
    for(i=0; i < json_object_array_length(fat); i++) {
        obj = json_object_array_get_idx(fat, i);
        sno = json_object_get_int(obj);
        printf("%d\n", sno);
    }
}

还有json常用的一些函数:

(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

 




0 0
原创粉丝点击