I'm requesting data from an api that isn't configured properly.
It serves as text/html, but when I run JSON.parse(data) I get an parse error. and I do data.trade
it says undefined.
If I just echo the data it looks like this (sample, not the full object):
"{\"buyOrder\":[{\"price\":\"5080.000000\"}]}"
Here is the url in question: http://www.btc38.com/trade/getTradeList.php?coinname=BTC
I'm using request
module to fetch the data.
How would I convert this string into a JSON object?
Here is the request:
var url = 'http://www.btc38.com/trade/getTradeList.php?coinname=BTC'
, json = true;
request.get({ url: url, json: json, strictSSL: false, headers: { 'User-Agent' : 'request x.y' } }, function (err, resp, data) {
c.log(data.trade); //undefined
});
Trimming the string got everything working well for me:
Output (truncated):