所以我一直在努力编写一个C程序,可以访问像www.cryptsy.com这样的各种加密货币网站的API,我有cURL部分计算出来并给出了程序吐出JSON数据到一个char *变量,它看起来是这样的:用C语言解析JSON与詹森C
{"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数据给我,让我决定尝试用杨松做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);
但程序只是失败,说明根不是一个数组,所以它在JSON代码启动后的第二条if语句中死亡。我注意到jansson有它自己的“request(url)”函数,你可以使用它来向服务器请求JSON数据,但为了获得我得到的数据,需要发送一个密钥/ API密钥和散列他们用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