/**
 @file
 @copyright COPYRIGHT ⓒ2017 SUKSU COMPUTER. ALL RIGHTS RESERVED. <br/>
 @note
 @author
 @section history Major History

 

* 어떤 JSON데이타를 자동으로 파싱하려고 하면, 굉장히 복잡하다. 우리는 REQUEST와 RESPONSE에 서로 규약되어 있는

데이타의 포맷에 맞춰서 JSON함수를 이용해, 데이타를 파싱하면 된다.

이는 소켓통신에서, 데이타의 규약과 마찬가지라고 보면 될것이다. 즉 RESPONSE JSON데이타의 포맷과 형식을 미리알고 있어야한다.
​     


​      */

 

 

#include <json-c/json.h>    //REDHAT LINUX계열
#include <stdio.h>
#include <string.h>

 

//컴파일방법

//gcc json2.c -ljson-c -o json2

 

char buf[1204L * 128L];

 

int main(int argc, char *argv[]) 
{
     FILE *fp = fopen(argv[1], "r");
 
     if(argc != 2)
     {
        printf("gcc json2.c -lcrypto -lcurl -ljson-c -o json2\n");
        return(-1);
     }
 
     if(NULL == fp)
     {
        fprintf(stderr, "fopen() failed");
        return -1;
     }

     int n = fread(buf, 1, sizeof(buf), fp);
     buf[n] = 0;

     struct json_object *jobj = NULL;

     jobj = json_tokener_parse(buf);    // ALREADY FORMATTING, SO, USE ONLY
     if(NULL == jobj) {
        return -1;
     }
 
     char KEY[100], SUBKEY[100];

     memset(KEY, 0x00, sizeof(KEY));
     strcpy(KEY, "result");
     printf("[%s][%s]\n", KEY, json_object_get_string(json_object_object_get(jobj, KEY)) );
 
     memset(KEY, 0x00, sizeof(KEY));
     strcpy(KEY, "reason");
     printf("[%s][%s]\n", KEY, json_object_get_string(json_object_object_get(jobj, KEY)) );
 
     struct json_object *pdata = NULL;
     if(json_object_object_get_ex(jobj, "summary", &pdata)) 
     {
        memset(KEY, 0x00, sizeof(KEY));
        strcpy(KEY, "totalpaid");
        printf("[%s][%s]\n", KEY, json_object_get_string(json_object_object_get(pdata, KEY)) );
  
        memset(KEY, 0x00, sizeof(KEY));
        strcpy(KEY, "totalreceived");
        printf("[%s][%s]\n", KEY, json_object_get_string(json_object_object_get(pdata, KEY)) );
  
        memset(KEY, 0x00, sizeof(KEY));
        strcpy(KEY, "totalunpaid");
        printf("[%s][%s]\n", KEY, json_object_get_string(json_object_object_get(pdata, KEY)) );
  
        memset(KEY, 0x00, sizeof(KEY));
        strcpy(KEY, "totalunreceived");
        printf("[%s][%s]\n", KEY, json_object_get_string(json_object_object_get(pdata, KEY)) );
    } 

    //struct json_object *pdata = NULL;
    if(json_object_object_get_ex(jobj, "data", &pdata)) 
    {
        int i;
        //int  json_object_array_length (struct json_object *obj) 
        for (i = 0; i < json_object_array_length(pdata); i++) 
        {
            //struct json_object *  json_object_array_get_idx (struct json_object *obj, int idx) 
            struct json_object *tmp = json_object_array_get_idx(pdata, i);
  
            printf("-----------------------------------\n");
            /*
            {"tradedate":"20170124",
             "settledate":"20170124",
             "cdrefer":"CN-20170124-01027",
             "amount":264554.830000,
             "status":"Received"
            },
            */
            memset(KEY, 0x00, sizeof(KEY));
            strcpy(KEY, "tradedate");
            printf("[%s][%s]", KEY, json_object_get_string(json_object_object_get(tmp, KEY)) );

            memset(KEY, 0x00, sizeof(KEY));
            strcpy(KEY, "settledate");
            printf("[%s][%s]", KEY, json_object_get_string(json_object_object_get(tmp, KEY)) );

            memset(KEY, 0x00, sizeof(KEY));
            strcpy(KEY, "cdrefer");
            printf("[%s][%s]", KEY, json_object_get_string(json_object_object_get(tmp, KEY)) );

            memset(KEY, 0x00, sizeof(KEY));
            strcpy(KEY, "amount");
            printf("[%s][%s]", KEY, json_object_get_string(json_object_object_get(tmp, KEY)) );

            memset(KEY, 0x00, sizeof(KEY));
            strcpy(KEY, "status");
            printf("[%s][%s]", KEY, json_object_get_string(json_object_object_get(tmp, KEY)) );

            printf("\n");
       }
   }
   return 0;
}

 

 

 

+ Recent posts