Reputation: 924
So I have this JSON
https://bitcoinpayflow.com/orders{"order":{"bitcoin_address":"1NwKSH1DJHhobCeuwxNqdMjK5oVEZBFWbk"}}
No I want to reference the bitcoin_address
So first I strip away the string at the beginning
var stripped = data.substring(33);
alert(stripped);
var btc = stripped.orders.bitcoin_address;
alert(btc);
I get the first alert, but not the second. Any idea why?
Upvotes: 1
Views: 386
Reputation: 6025
I would use JSON.parse as follows.
$.post('php/whatever.php',{data:dS},function(res){
var o=JSON.parse(res);
var bitcoins=o.order.bitcoinaddress;
},"text");
Upvotes: 1
Reputation: 5556
The easiest way to decode json - string - use eval
var bitcoins = eval('(' + json_string + ')');
And access bitcoins['order']['bitcoin_address']
But it a bit unsafe. Upper method is more safer.
Upvotes: 1
Reputation: 166041
Because stripped
is still just a string. You need to parse it into an object. You can use the native JSON.parse
method to do this:
var stripped = JSON.parse(data.substring(33));
Also, you are referencing the orders
property, which doesn't exist. It's order
.
Note that JSON.parse
is not supported by older browsers. You can use this polyfill to make sure it's always available.
Upvotes: 2