0

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);

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Edited closing bracket..Will check this
Worked! And one more doubt...I am getting multiple values for the same div id. So How can I use it as separate one?
Sorry, don't understand bud. Can you clarify?
Actually, for transaction object, it is having same value for all the arrays. But, for value2 object, it is not showing the value because, it has ABCDE in first array and CDEFG in second array...
Not sure what you want to do.. Make a jsfiddle and I can help you out
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.