I’m calling a JSON object and in Firebug it returns this response :
"[{\"employee\":{\"account_id\":1,\"active\":true,\"activity_ical_hash\":\"af2d0f784ce28bc16d6fdf593d3e4bc7\",\"address\":null,\"admin\":false,\"all_tasks_ical_hash\":\"d4067eceea22b2f281c65f22ccc7820f\",\"always_send_daily_schedule\":true,\"api_token\":\"8d4ab012505392a25d1469e33945d9b05365eedb\",\"can_login\":true,\"cell_phone\":null,\"city\":null,\"created_at\":\"2012-02-20T09:41:27-08:00\",\"custom_datetime1\":null,\"custom_datetime10\":null,\"custom_datetime2\":null,\"custom_datetime3\":null,\"custom_datetime4\":null,\"custom_datetime5\":null,\"custom_datetime6\":null,\"custom_datetime7\":null,\"custom_datetime8\":null,\"custom_datetime9\":null,\"custom_number1\":null,\"custom_number10\":null,\"custom_number2\":null,\"custom_number3\":null,\"custom_number4\":null,\"custom_number5\":null,\"custom_number6\":null,\"custom_number7\":null,\"custom_number8\":null,\"custom_number9\":null,\"custom_text1\":null,\"custom_text10\":null,\"custom_text2\":null,\"custom_text3\":null,\"custom_text4\":null,\"custom_text5\":null,\"custom_text6\":null,\"custom_text7\":null,\"custom_text8\":null,\"custom_text9\":null,\"email_address\":\"mckenna_moore@schumm.org\",\"email_schedule_daily\":true,\"employee_number\":4,\"hashed_password\":\"cbc689313dfd6fd144f7df117c4f18e1627afde4\",\"hide_pricing\":null,\"home_phone\":null,\"id\":4,\"is_account_owner\":false,\"jobs_ical_hash\":\"d51abd7af0326083f9d25c4aacc828c0\",\"limit_access_to_assignments\":false,\"name\":\"Horacio Johnson\",\"notification_email_address\":\"tyrell.bartoletti@donnellyerdman.biz\",\"notification_mobile_host\":null,\"notification_mobile_number\":null,\"notify_on_tasks\":true,\"notify_on_tasks_by_sms\":true,\"notify_via_email\":true,\"notify_via_mobile\":false,\"pager\":null,\"public_tasks_ical_hash\":\"592b2d4150ef46821c2ae8df63ca686d\",\"remember_me_token\":null,\"remember_me_token_expires_at\":null,\"salt\":\"35f1a3c4\",\"show_setup_tab\":null,\"state\":null,\"tech\":true,\"updated_at\":\"2012-02-20T09:41:27-08:00\",\"username\":\"user_marguerite\",\"zip_code\":null}}]"
What syntax would be required to return the name attribute in each returned element?
I tried this, but it didn’t work
$.getJSON(window.location.pathname+'.json?employees='+$("input").attr('value'), function(data){
$available_names = []
$.each(data, function(i, val) {
$name = val.name;
$available_names.push($name);
});
});
Anyone know why this doesn’t work?
Update
Trying to add an error handler on this method and this won’t even run. It fails for some syntactical reason :
$.getJSON(window.location.pathname+'.json?employees='+$("input").attr('value'), function(data){
}).error(function() { alert("error"); });
Your data is in an Array structure. It appears as though it’s meant to be an Array of employee objects.
To get the name of each employee, you’d need to access the
employeeproperty of each object.…but if you’re building an Array, you could use
$.mapinstead…If it still isn’t working, then check to see if your JSON is being double encoded. There’s something about that Firebug output that makes me think that may be the case.
You can test this by having the response parsed again, like this…
If it starts working, then it is definitely being double encoded on the server, so you’d want to fix that.