/    Sign up×
Community /Pin to ProfileBookmark

Getting Multiple JSON values back in AJAX Script

I have a JSON string being returned from PHP that looks exactly like this:

[CODE]{“id”:”1″,”name”:”Bill Johnson”,”age”:”23″,”experience”:”7″,”dept”:”Web Developer”}{“id”:”3″,”name”:”Bob Smith”,”age”:”48″,”experience”:”26″,”dept”:”Lawyer”}[/CODE]

I’m searching an employee database by name, so “Bill Johnson” and “Bob Smith” will both show up if you just type “B”. That’s the JSON string that’s being returned, but I don’t know the syntax to iterate through each object and get all the values. Currently nothing displays until there is only ONE result, because this is my code:

[CODE]var result = “”;
result += data.name+”<br />”;
result += “Employee ID: “+data.id+”<br />”;
result += “Age: “+data.age+”<br />”;
result += “Experience: “+data.experience+” years <br />”;
result += “Department: “+data.dept+”<br />”;[/CODE]

I want this to be able to display up to 10 employees whose names match what you’re typing.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 21.2010 —  but I don't know the syntax to iterate through each object and get all the values. [/QUOTE]
<i>
</i>var JSON={"id":"1","name":"Bill Johnson","age":"23","experience":"7","dept":"Web Developer"};
for(prop in JSON){
alert(prop+' : '+JSON[prop])
}

[B]prop[/B] is just a custom name of variable. You may name it whichever. It returns all the properties of a certain object within a loop.
×

Success!

Help @Dean440 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...