Jasson库使用

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#include <stdio.h>
#include <jansson.h>
int main()
{
   
    json_t *ptJsRoot = json_pack("{s:s, s:{}, s:{}, s:{}}",
              "devid", "111",
              "card_lights_info",
              "left_smu_lights_info",
              "right_smu_lights_info");

    if( NULL == ptJsRoot)
    {
       printf("ptJsRoot pack failed!\n");
       return ;
    }

    json_t * ptJsTmp = NULL;
    //card_lights_info
    json_t *ptJsCardLighsInfo=NULL;
    ptJsCardLighsInfo = json_pack("{s:i, s:[]}","card_nums",3, "card_lights_detail");
    if( NULL == ptJsCardLighsInfo)
    {
      printf("ptJsCardLighsInfo pack failed!\n");
      return ;
    }

    ptJsTmp = json_object_get(ptJsRoot, "card_lights_info");
    if( NULL == ptJsTmp)
    {
      printf("ptJsTmp card_lights_info not exist!\n");
      return ;
    }

   json_object_update_missing(ptJsTmp, ptJsCardLighsInfo);
   json_decref(ptJsCardLighsInfo);
  

   ptJsTmp = json_object_get(ptJsRoot, "card_lights_info");
   if( NULL == ptJsTmp )
   {
     printf("card_lights_info not exist!\n");
   }

   printf("card_nums: %d\n", json_integer_value( json_object_get(ptJsTmp, "card_nums") ));


    ptJsTmp = json_object_get(ptJsTmp, "card_lights_detail");
    if( NULL == ptJsTmp)
    {
      printf("ptJsTmp card_lights_detail not exist!\n");
      return ;
    }
      //card_lights_detail
    int i = 0;
    int j = 0;
    for( i= 0; i < 3; i++)
    {
        json_t *ptJsCardLightsDetail = json_pack("{s:s,s:i,s:[]}",
                "card_guid","11111111",
                "lights_num",i,
                "lights_info");
       if( NULL == ptJsCardLightsDetail)
      {
           printf("ptJsCardLightsDetail json_pack failed!\n");
           return ;
      }

      json_t *ptJsLinghtsInfoTmp = json_object_get(ptJsCardLightsDetail, "lights_info");
      if( NULL == ptJsLinghtsInfoTmp )
      {
         printf("lights_info is not exist!\n");
         return ;
      }

      for( j = 0; j < i; j++)
      {
          json_t *ptJsLightsInfo = json_pack("{s:i,s:i}",
                                  "lights_type",i,
                                  "lights_color",j);
          if( NULL == ptJsLightsInfo )
          {
            printf("ptJsCardLightsDetail json_pack failed!\n");
            return;
          }

          json_array_append(ptJsLinghtsInfoTmp, ptJsLightsInfo);
          json_decref(ptJsLightsInfo);
      }
      json_array_append(ptJsTmp, ptJsCardLightsDetail);
      json_decref(ptJsCardLightsDetail);
    }


    printf("--------------------------------------\n");
    for( i = 0; i< json_array_size(ptJsTmp); i++)
    {
       json_t * ptCardinfo = json_array_get(ptJsTmp, i);
       printf("lights_num: %d\n", json_integer_value(json_object_get(ptCardinfo, "lights_num")));
       printf("card_guid: %s\n", json_string_value(json_object_get(ptCardinfo, "card_guid")));
       printf("----------\n");
       json_t *ptJsLightsTmp = json_object_get(ptCardinfo, "lights_info");
       if( NULL == ptJsLightsTmp )
       { 
         printf("lights_info is not exist!\n");
         return ;
       }

       for( j = 0; j < json_integer_value(json_object_get(ptCardinfo, "lights_num")); j++ )
       {
          json_t * ptJsLightsInfo = json_array_get(ptJsLightsTmp, j);
          printf("lights_type: %d\n", json_integer_value(json_object_get(ptJsLightsInfo, "lights_type")));
          printf("lights_color: %d\n", json_string_value(json_object_get(ptJsLightsInfo, "lights_color")));
          printf("------------------------\n");
       }
    }


     
    //  json_t * ptJsTmp1 = NULL;
    //  ptJsTmp1 = json_object_get(ptJsRoot, "card_lights_info");
    // if( NULL == ptJsTmp1 )
    // {
    //   printf("card_lights_info not exist!\n");
    // }
    // printf("--------------------------------------\n");
    // printf("card_nums: %d\n", json_integer_value( json_object_get(ptJsTmp1, "card_nums") ));

    //  ptJsTmp1 = json_object_get(ptJsTmp1, "card_lights_detail");

    // if( NULL == ptJsTmp1 )
    // {
    //   printf("card_lights_detail not exist!\n");
    // }

    // printf("--------------------------------------\n");
    // for( i = 0; i< json_array_size(ptJsTmp1); i++)
    // {
    //    json_t * ptCardinfo1 = json_array_get(ptJsTmp1, i);
    //    printf("lights_num: %d\n", json_integer_value(json_object_get(ptCardinfo1, "lights_num")));
    //    printf("card_guid: %s\n", json_string_value(json_object_get(ptCardinfo1, "card_guid")));
    // }

}

编译:gcc -o test test.c -ljansson -Wl,-rpath=/usr/local/lib/

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:c#判断网卡是否可用

下一篇:npoi 导出excel