/    Sign up×
Community /Pin to ProfileBookmark

Having difficulty parsing through a nested JSON Object using Prototype Framework

This is an AJAX/Prototype problem, which is independent of the server-side scripting language I’m currently using. I have the following multi-demensional nested array, which I have successfuly turned into a JOSN Object as such:

[CODE][{“PlayerName”:”Ron Artest”,”Positn”:”Forward”,”Height”:”6-7 l”,”Weight”:”260″,”College”:”St. Johns”},
{“PlayerName”:”Kobe Bryant”,”Positn”:”Guard”,”Height”:”6-6 l”,”Weight”:”205″,”College”:”Lower Marion High Sc”},
{“PlayerName”:”Andrew Bynum”,”Positn”:”Center”,”Height”:”7-0 l”,”Weight”:”285″,”College”:”St. Josephs”},
{“PlayerName”:”Pau Gasol”,”Positn”:”Forward”,”Height”:”7-0 l”,”Weight”:”250″,”College”:”Spain”},
{“PlayerName”:”Derick Fisher”,”Positn”:”Guard”,”Height”:”6-1 l”,”Weight”:”210″,”College”:”Arkansas-Little Rock”}][/CODE]

I’m currently using the firbug console and the following code is telling me that I have a successful Object available to me:

[CODE]var request = new Ajax.Request(‘dropDownMenu_ajax.php’,
{
onSuccess: function(request)
{
var players = request.responseJSON;

console.log(players);

},
method: ‘get’,
parameters:
{
team: ‘LA Lakers’
}
});[/CODE]

The issue occurs when it is time for me to parse this data and render it in a browser where I can make some use of it. The below for loop give me results, but clumps everything together:

[CODE]var request = new Ajax.Request(‘dropDownMenu_ajax.php’,
{
onSuccess: function(request)
{
var players = request.responseJSON;

console.log(players);

for (var team in players){
for (var member in players[team]){
$(‘workaroundOutput’).insert(players[team][member]);}}

},
method: ‘get’,
parameters:
{
team: ‘LA Lakers’
}
});[/CODE]

The above code gives me the following, all jumbled together:

[CODE]Ron ArtestForward6-7 l260St. JohnsKobe BryantGuard6-6 l205Lower Marion High ScAndrew BynumCenter7-0 l285St. JosephsPau GasolForward7-0 l250SpainDerick FisherGuard6-1 l210Arkansas-Little Rock[/CODE]

When I try and separate the result with a <br/> my using

[CODE]$(‘workaroundOutput’).insert(players[team][member] + ‘<br/>’);}}[/CODE]

instead of

[CODE]$(‘workaroundOutput’).insert(players[team][member]);}}[/CODE]

I get something crazy. I get the results, but after that it seems like I’m getting get all the prototype extensions to the Array object along with an empty object

[CODE]Ron Artest
Forward
6-7 l
260
St. Johns
Kobe Bryant
Guard
6-6 l
205
Lower Marion High Sc
Andrew Bynum
Center
7-0 l
285
St. Josephs
Pau Gasol
Forward
7-0 l
250
Spain
Derick Fisher
Guard
6-1 l
210
Arkansas-Little Rock
[object Object]
function () { var names = this.toString().match(/^[s(]*function[^(]*(([^)]*))/)[1].replace(/s+/g, “”).split(“,”); return names.length == 1 && !names[0] ? [] : names; }
function () { if (arguments.length < 2 && Object.isUndefined(arguments[0])) { return this; } var __method = this, args = $A(arguments), object = args.shift(); return function () {return __method.apply(object, args.concat($A(arguments)));}; }
function () { var __method = this, args = $A(arguments), object = args.shift(); return function (event) {return __method.apply(object, [event || window.event].concat(args));}; }[/CODE]

How would I properly parse and traverse this JSON Object.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @cd_gary spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...