Greg
Greg

Reputation: 8784

JSON Deserialization Issue

I know I'm just missing something stupid here. Anyone see what I'm doing wrong?

http://jsfiddle.net/xZvYW/

HTML:

<div id="pageheader">
    <h1></h1>
    <div></div>
</div>

Javascript/jQuery:

var si = [{
"courseid": "1",
"coursename": "MISY431/432",
"coursedescription": "Project Management and MIS Projects",
"sectionid": "1",
"sectionname": "",
"semesterid": "1",
"semestername": "Fall 2011",
"semesterstarttimestamp": "9/1/2011 12:00:00 AM",
"phaseid": "3",
"phasename": "Open to Students",
"phasedescription": "Students may view accepted projects and submit project team requests"}];

$("#pageheader h1").html(si.semestername + " " + si.coursename + " " + si.sectionname);
$("#pageheader div").html("Current Phase: <strong>" + si.phasename + "</strong> <em>" + si.phasedescription + "</em>");

console.log(si);

Renders as:

undefined undefined undefined
Current Phase: undefined undefined

Thanks, - Greg

Upvotes: 0

Views: 104

Answers (2)

deceze
deceze

Reputation: 522625

Your si variable contains an array with one object ([{}]). The correct syntax would be si[0].semestername etc. Or you can leave out the array wrapper if you don't need it.

Upvotes: 1

GregL
GregL

Reputation: 38151

You have defined si as an array, but are not indexing into that array when you are accessing the variable.

Remove the square brackets around the definition of si and you should be good to go.

Upvotes: 4

Related Questions