UI_Dev
UI_Dev

Reputation: 3427

How to set json object from json array in jquery?

Here I am getting JSON object in the following format.

{"userid":"12345","cmpyname":"stackoverflow",
"starrays":[{"transaction":"7272785","value2":"ABCDE"},    
{"transaction":"4774585","value2":"CDEFG"}],"value3":"95345"}

Explanation:

For Particular userid (12345), it will return some set of arrays - starrays with same object names like transaction, value2

Here, I am setting userid and cmpyname like below.

<div id="t_userid"></div>
<div id="t_cmpy"></div>

In ajax, I am getting JSON object and setting the values to the div elements like below.

$('#userid').html(data.userid);

$('#t_cmpy').html(data.cmpyname);

Now, I need to know how can I get the values of transaction and value2 objects from the array?.

<div id="t_trans"></div>
<div id="t_val2"></div>

I tried like this. But didn't work.

$('#t_trans').html(data.starrays.transaction);

$('#t_val2').html(data.starrays.value2);

And, here mutiple values are there in the array, how can I set to my div one by one. (not appending values)

Edit:

{"userid":"12345","cmpyname":"stackoverflow",
"starrays":[{"transaction":"7272785","addr":"ABCDE"},    
{"transaction":"4774585","addr":"ABCDE"}],"value3":"95345"}

Here, I am having same value in addr object as ABCDE.

In this case, if I use like below it is working

$('#divid1').html(data.starrays[1].addr);

But, I am having different value in transaction as 7272785, 4774585.

In this case, if I use like below it is not working

$('#divid2').html(data.starrays[0].transaction);

Upvotes: 0

Views: 134

Answers (1)

Graham T
Graham T

Reputation: 966

The array in your code doesn't have a closing bracket. I assume you just pasted a portion though..

You need to reference the array index:

$('#t_trans').html(data.starrays[0].transaction);

Upvotes: 1

Related Questions