gpt4 book ai didi

c - 在 C 中用 jansson 解析 JSON

转载 作者:太空狗 更新时间:2023-10-29 15:02:20 27 4
gpt4 key购买 nike

所以我一直致力于用 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 不是一个数组,所以它在 J​​SON 代码开始后的第二个 if 语句中死亡。我注意到 jansson 有自己的“request(url)”函数,您可以使用它向服务器发出 JSON 数据请求,但要获取我正在获取的数据,您需要发送 key /API key 和哈希他们用SHA512来得到一个有效的响应,这个函数好像没有这个能力。

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

编辑::

好的,所以我尝试将代码更改为只解析“根”对象,但它似乎只通过“返回”和“成功”键。也许我仍然不完全理解,但这是我现在正在使用的代码来尝试解析所有内容,我阅读了 jansson API 指南,它说如果你想遍历对象中的每个键:值对,请使用迭代器,如下所示:

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

最佳答案

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

关于c - 在 C 中用 jansson 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com