ECasio ECasio - 5 months ago 8
JSON Question

How can I use the first entry of a JSON data array

I got this JSON as data:

data = {"livemode":false,"pending":
[{"amount":0,"currency":"jpy"},{"amount":8121,"currency":"usd"}],
"available":
[{"amount":-400,"currency":"jpy"},{"amount":99601,"currency":"usd"}]}


now I want to convert this into a more readable text something like this:

"Pending :" + "\n" + "Amount :" + data.pending[0].amount + "\n" + data.pending[0].currency;


So I want to give back the amount of 0 with the currency of jpy but all I get is an Error or undefined, what I'm doing wrong here?

onRetrieveBalancePressed : function() {
var that = this;
jQuery.ajax({
type : "GET",
contentType : "application/json",
url : "https:" + "/retrieve",
processData: true,
dataType : "text",
async: false,

success : function(data, textStatus, jqXHR) {
var sResult = "Balance successfully retrieved! \n"
+"Pending :" + "\n"
+"livemode :" + data.pending[0].amount + "\n"

+"Currency : " + data.pending[0].currency;

Answer

First just try to convert your data to JSON using var obj = JSON.parse(data);

then try to get data from obj.

In case of jquery, you can also try this:

var obj = jQuery.parseJSON(data);
Comments