在C中用jansson解析JSON

时间:2013-12-19 02:22:17

标签: c json text web

所以我一直在用C语言编写一个程序,可以访问各种加密货币网站的API,比如www.cryptsy.com,我找到了cURL部分并让程序将JSON数据吐出到char *中看起来像这样的变量:

{"success":"1","return":{"balances_available":{"ALF":"0.00000000","AMC":"0.00000000","ADT":"0.00000000","ANC":"0.00000000","ARG":"0.00000000","ASC":"0.00000000","BQC":"0.00000000","BTB":"0.00000000","BTC":"0.00535673","BTG":"0.00000000","CAP":"0.00000000","BTE":"0.00000000","CSC":"0.00000000","CNC":"0.00000000","COL":"0.00000000","CPR":"0.00000000","CLR":"0.00000000","CMC":"0.00000000","CRC":"0.00000000","BUK":"0.00000000","CGB":"0.00000000","Points":"0.00911000","DVC":"0.00000000","DMD":"0.00000000","DGC":"0.00000000","DBL":"0.00000000","ELC":"0.00000000","ELP":"0.00000000","EMD":"0.00000000","EZC":"0.00000000","FST":"0.00000000","FTC":"0.00000000","FLO":"0.00000000","FRK":"0.00000000","FRC":"0.00000000","GLX":"0.00000000","GME":"0.00000000","GLC":"0.00000000","GLD":"0.00000000","GDC":"0.00000000","HBN":"0.00000000","HYC":"0.00000000","IFC":"0.00000000","IXC":"0.00000000","XJO":"0.00000000","JKC":"0.00000000","KGC":"0.00000000","LTC":"0.00000000","LK7":"0.00000000","LKY":"0.00000000","MST":"0.00000000","MEC":"0.00000000","MEM":"0.00000000","MNC":"0.00000000","NMC":"0.00000000","NAN":"0.00000000","NEC":"0.00000000","NET":"0.00000000","NBL":"0.00000000","NRB":"0.00000000","NVC":"0.00000000","ORB":"0.00000000","PYC":"0.00000000","PPC":"0.00000000","CENT":"0.00000000","PHS":"0.00000000","PXC":"0.00000000","XPM":"0.00000000","PTS":"0.00000000","QRK":"0.00000000","RED":"0.00000000","RYC":"0.00000000","SRC":"0.00000000","SXC":"0.00000000","SPT":"0.00000000","SBC":"0.00000000","STR":"0.00000000","TAG":"0.00000000","TEK":"0.00000000","TRC":"0.00000000","TIX":"0.00000000","WDC":"0.00000000","XNC":"0.00000000","YAC":"0.00000000","ZET":"0.00000000"},"balances_hold":{"ALF":"0.00000000","AMC":"0.00000000","ADT":"0.00000000","ANC":"0.00000000","ARG":"0.00000000","ASC":"0.00000000","BQC":"0.00000000","BTB":"0.00000000","BTC":"0.00000000","BTG":"0.00000000","CAP":"0.00000000","BTE":"0.00000000","CSC":"0.00000000","CNC":"0.00000000","COL":"0.00000000","CPR":"0.00000000","CLR":"0.00000000","CMC":"0.00000000","CRC":"0.00000000","BUK":"0.00000000","CGB":"0.00000000","Points":"0.00000000","DVC":"0.00000000","DMD":"0.00000000","DGC":"0.00000000","DBL":"0.00000000","ELC":"0.00000000","ELP":"0.00000000","EMD":"0.00000000","EZC":"0.00000000","FST":"0.00000000","FTC":"0.00000000","FLO":"0.00000000","FRK":"0.00000000","FRC":"0.00000000","GLX":"0.00000000","GME":"0.00000000","GLC":"0.00000000","GLD":"0.00000000","GDC":"0.00000000","HBN":"0.00000000","HYC":"0.00000000","IFC":"0.00000000","IXC":"0.00000000","XJO":"0.00000000","JKC":"0.00000000","KGC":"0.00000000","LTC":"1.10231195","LK7":"0.00000000","LKY":"10.36912507","MST":"0.00000000","MEC":"0.00000000","MEM":"0.00000000","MNC":"0.00000000","NMC":"0.00000000","NAN":"0.00000000","NEC":"0.00000000","NET":"0.00000000","NBL":"0.00000000","NRB":"0.00000000","NVC":"0.00000000","ORB":"0.00000000","PYC":"0.00000000","PPC":"0.00000000","CENT":"0.00000000","PHS":"0.00000000","PXC":"0.00000000","XPM":"0.00000000","PTS":"0.00000000","QRK":"0.00000000","RED":"0.00000000","RYC":"0.00000000","SRC":"0.00000000","SXC":"0.00000000","SPT":"0.00000000","SBC":"0.00000000","STR":"0.00000000","TAG":"0.00000000","TEK":"0.00000000","TRC":"0.83964122","TIX":"0.00000000","WDC":"0.00000000","XNC":"0.00000000","YAC":"0.00000000","ZET":"0.00000000"},"servertimestamp":1387347714,"servertimezone":"EST","serverdatetime":"2013-12-18 01:21:54","openordercount":3}}

对我来说看起来像是有效的JSON数据所以我决定尝试使用jansson进行JSON解析/解码,在我的代码中看起来像这样:

char *data;

if ( argc < 2 )
{
    fprintf( stderr, "Must provide URL to fetch.\n" );
    return 1;
}
//data = mmpool_api( argv[1] );
data = cryptsy_api( argv[1] );

json_t *root;
json_error_t error;

root = json_loads( data, 0, &error );
if ( !root )
{
    fprintf( stderr, "error: on line %d: %s\n", error.line, error.text );
    return 1;
}
if ( !json_is_array(root) )
{
    fprintf( stderr, "error: root is not an array\n" );
    json_decref(root);
    return 1;
}

for( int i = 0; i < json_array_size(root); i++ )
{
    json_t *data, *success, *returned, *bal_avail;
    const char *balance;

    data = json_array_get( root, i );
    if ( !json_is_object(data) )
    {
        fprintf( stderr, "error: data %d is not an object\n", i + 1 );
        json_decref(root);
        return 1;
    }

    success = json_object_get( data, "success" );
    if ( !json_is_number(success))
    {
        fprintf( stderr, "error: %d: success is not a number", i+1 );
        json_decref(root);
        return 1;
    }

    returned = json_object_get(data, "return" );
    if ( !json_is_object(returned) )
    {
        fprintf( stderr, "error: %d: return is not an object", i+1  );
        json_decref(root);
        return 1;
    }

    bal_avail = json_object_get( returned, "balances_available" );
    if ( !json_is_array( bal_avail ))
    {
        fprintf( stderr, "error: %d: bal_avail is not an array", i+1 );
        json_decref(root);
    }

    balance = json_string_value(bal_avail);
    printf( "%s %.*s\n",
        json_string_value(bal_avail),
        newline_offset(balance),
        balance);
}
json_decref(root);

但是程序只是失败说明root不是一个数组,所以它在JSON代码启动后在第二个if语句中死掉。我注意到jansson有自己的“请求(url)”函数,您可以使用该函数向服务器请求JSON数据,但是为了获取我需要的数据,您需要发送密钥/ API密钥和哈希他们使用SHA512获得有效响应,此功能似乎没有这个功能。

我知道C还有其他用于解码JSON数据的库,但我只是想在切换到JSON-C或其他库之前确保我没有做错。谢谢

EDIT ::

好吧所以我试图改变代码只是解析'root'对象,但它似乎只通过“返回”和“成功”键..也许我仍然不完全理解,但这里是我正在使用的代码试图解析所有内容,我阅读了jansson API指南,如果你想要遍历对象中的每一个key:value对,它会说使用迭代器:

char *data;

if ( argc < 2 )
{
    fprintf( stderr, "Must provide URL to fetch.\n" );
    return 1;
}
//data = mmpool_api( argv[1] );
data = cryptsy_api( argv[1] );

json_t *root;
json_error_t error;

root = json_loads( data, 0, &error );
if ( !root )
{
    fprintf( stderr, "error: on line %d: %s\n", error.line, error.text );
    return 1;
}

const char *key;
json_t *value;

void *iter = json_object_iter( root );
while( iter )
{
    key = json_object_iter_key(iter);
    value = json_object_iter_value(iter);

    printf("Key: %s, Value: %f\n", key, json_real_value(value) );

    iter = json_object_iter_next(root, iter);
}

json_decref(root);

但它是这样的(只有前两个项是地址,它们是相反的顺序):

键:返回,值:0.000000 关键:成功,价值:0.000000

1 个答案:

答案 0 :(得分:6)

JSON字符串的根元素是一个对象,而不是一个数组。 JSON中的数组用方括号[]表示,对象用大括号{}表示。如果您已经知道要访问哪些键,则无需迭代“object”根元素,只需使用json_object_get(root, "keyname");,直到您访问了所需的所有值。