• JSON-C库的使用(1)Json对象数组的解析


    版本:json-c-0.9.tar.gz

    参考:http://blog.csdn.net/mengyafei43/article/details/38494139

    示例代码:

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <unistd.h>
      5 #include "json.h"
      6 
      7 void encodeJson();
      8 void decodeJson();
      9 
     10 int main(int argc, char **argv)
     11 {
     12     int ret = 0;
     13     
     14     encodeJson();
     15     
     16     decodeJson();
     17 
     18     return ret;
     19 }
     20 
     21 static char *json_type_to_name(int json_type)
     22 {
     23     char json_type_name[128];
     24 
     25     memset(json_type_name, 0x00, sizeof(json_type_name));
     26     switch(json_type)
     27     {
     28         case json_type_null:
     29             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_null");
     30             break;
     31             
     32         case json_type_boolean:
     33             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_boolean");
     34             break;
     35             
     36         case json_type_double:
     37             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_double");
     38             break;
     39             
     40         case json_type_int:
     41             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_int");
     42             break;
     43         
     44         case json_type_object:
     45             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_object");
     46             break;
     47             
     48         case json_type_array:
     49             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_array");
     50             break;
     51         
     52         case json_type_string:
     53             snprintf(json_type_name, sizeof(json_type_name), "%s", "json_type_string");
     54             break;
     55     }
     56     
     57     return json_type_name;
     58 }
     59 
     60 static void getFieldValue(struct json_object *child_obj, const char *field)
     61 {
     62     struct json_object *obj = json_object_object_get(child_obj, field);
     63 
     64     enum json_type obj_type = json_object_get_type(obj);
     65     //printf("%s json_type is %s
    ", field, json_type_to_name(obj_type));
     66     
     67     if (obj_type == json_type_int)
     68     {
     69         printf("%s=%d
    ", field, json_object_get_int(obj));
     70     }
     71     else if (obj_type == json_type_string)
     72     {
     73         printf("%s=%s
    ", field, json_object_get_string(obj));
     74     }
     75 }
     76 
     77 void decodeJson()
     78 {
     79     const char *json_string = "{ 
    
     80         "userinfo": [ 
    
     81         { "pin": "10000", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     82         { "pin": "10001", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     83         { "pin": "10002", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     84         { "pin": "10003", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     85         { "pin": "10004", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     86         { "pin": "10005", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     87         { "pin": "10006", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     88         { "pin": "10007", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     89         { "pin": "10008", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 }, 
    
     90         { "pin": "10009", "cardno": "123456789", "password": "123456", "name": "paul", "Privilege": 14, "flag": 1 } 
    
     91         ] }";
     92     
     93     int ret = 0;
     94     int i = 0;
     95     struct json_object *root_obj = NULL;
     96     struct json_object *arry_obj = NULL;
     97     struct json_object *child_obj = NULL;
     98     struct lh_entry *entry = NULL;
     99     char *key = NULL;
    100     
    101     root_obj = json_tokener_parse(json_string);
    102     entry = json_object_get_object(root_obj)->head;
    103     if (entry)
    104     {
    105         key = (char *)entry->k;
    106         arry_obj = (struct json_object *)entry->v;
    107         
    108         if (json_type_array != json_object_get_type(arry_obj) 
    109             && json_type_object != json_object_get_type(arry_obj))
    110         {
    111             return -1;
    112         }
    113         
    114         printf("key = %s
    ", key);
    115         printf("array_obj = %s
    ", json_object_to_json_string(arry_obj));
    116         
    117         if (json_type_array == json_object_get_type(arry_obj))
    118         {
    119             for(i = 0; i < json_object_array_length(arry_obj); i++)
    120             {
    121                 child_obj = json_object_array_get_idx(arry_obj, i);
    122                 //printf("child_obj = %s
    ", json_object_to_json_string(child_obj));
    123                 
    124                 getFieldValue(child_obj, "pin");
    125                 getFieldValue(child_obj, "cardno");
    126                 getFieldValue(child_obj, "password");
    127                 getFieldValue(child_obj, "name");
    128                 getFieldValue(child_obj, "Privilege");
    129                 getFieldValue(child_obj, "flag");
    130                 printf("
    ");
    131             }
    132         }
    133         else if (json_type_object == json_object_get_type(arry_obj))
    134         {
    135             
    136         }
    137     }
    138     
    139 }
    140 
    141 void encodeJson()
    142 {
    143     int i = 0;
    144     struct json_object *root_obj = NULL;
    145     struct json_object *child_obj = NULL;
    146     struct json_object *array_obj = NULL;
    147     char *tmpStr = NULL;
    148     
    149     root_obj = json_object_new_object(); 
    150     array_obj = json_object_new_array();
    151     
    152     for(i = 0; i < 10; i++)
    153     {
    154         child_obj = json_object_new_object();
    155     
    156         json_object_object_add(child_obj, "pin", json_object_new_string("10000"));
    157         json_object_object_add(child_obj, "cardno", json_object_new_string("123456789"));
    158         json_object_object_add(child_obj, "password", json_object_new_string("123456"));
    159         json_object_object_add(child_obj, "name", json_object_new_string("paul"));
    160         json_object_object_add(child_obj, "Privilege", json_object_new_int(14));
    161         json_object_object_add(child_obj, "flag", json_object_new_int(1));
    162         
    163         json_object_array_add(array_obj, child_obj);
    164         child_obj = NULL;
    165     }
    166     
    167     json_object_object_add(root_obj, "userinfo", array_obj);
    168     
    169     tmpStr = json_object_to_json_string(root_obj);
    170     printf("%s
    ", tmpStr);
    171 }

    运行结果:

  • 相关阅读:
    艾伟:WCF中通过Dispose有效实现重用 狼人:
    艾伟:用 IIS 7、ARR 與 Velocity 建置高性能的大型网站 狼人:
    艾伟:表达式树和泛型委托 狼人:
    艾伟:jQuery性能优化指南(2) 狼人:
    艾伟:在Windows Mobile上实现自动拼写和匹配建议 狼人:
    艾伟:Web.config配置文件详解 狼人:
    艾伟:对 String 的几个错误认识 狼人:
    艾伟:ASP.NET安全问题--Forms验证的具体介绍(上篇) 狼人:
    艾伟:基于web信息管理系统的权限设计分析和总结 狼人:
    艾伟:[你必须知道的.NET]第三十一回,深入.NET 4.0之,从“新”展望 狼人:
  • 原文地址:https://www.cnblogs.com/paullam/p/4522434.html
Copyright © 2020-2023  润新知