json file and jquery iteration

I need to create a simple list (UL) from a multidimensional json file but am getting stuck.

This is the json file:
{
	"groundfloor" : { "floorname" : "Ground Floor",
		"reception" : { "roomtypename" : "Reception"			
		},

		"meetingroom" : { "roomtypename" : "Meeting Room",
			"00.12" : { "name" : "00.12", "coords" : [1045,385]},
			"00.13" : { "name" : "00.13", "coords" : [969,394]},
			"00.14" : { "name" : "00.14", "coords" : [969,394]},
			"00.15" : { "name" : "00.15", "coords" : [969,394]},
			"00.16" : { "name" : "00.16", "coords" : [888,365]},
			"00.18" : { "name" : "00.18", "coords" : [808,365]},
			"00.19" : { "name" : "00.19", "coords" : [695,370]},
			"00.20" : { "name" : "00.20", "coords" : [580,360]},
			"00.bhs" : { "name" : "Hospital Suite", "coords" : [575,100]},
			"00.23" : { "name" : "00.23", "coords" : [160,385]}
		},

		"itsuite" : { "roomtypename" : "IT Suite",
			"00.28" : { "name" : "00.28", "coords" : [50,250]}
		},

		"catering" : { "roomtypename" : "Catering"
		},

		"toilets" : { "roomtypename" : "Toilets"
		},

		"exits" : { "roomtypename" : "Exits"
		}
	},




	"1stfloor" : { "floorname" : "1st Floor",
		"meetingroom" : { "roomtypename" : "Meeting Room",
			"01.01" : { "name" : "1.01", "coords" : [1015,370]},
			"01.08" : { "name" : "Cathays", "coords" : [940,370]},
			"01.09" : { "name" : "Ely", "coords" : [850,370]},
			"01.10" : { "name" : "Fairwater", "coords" : [750,370]},
			"01.11" : { "name" : "Gabalfa", "coords" : [670,370]},
			"01.13" : { "name" : "Grangetown", "coords" : [610,370]},
			"01.14" : { "name" : "Heath", "coords" : [550,370]},
			"01.16" : { "name" : "Harvard Room", "coords" : [115,390]}
		},
		"itsuite" : { "roomtypename" : "IT Suite"
		},
		"teachingroom" : { "roomtypename" : "Teaching Room",
			"01.15" : { "name" : "1.15", "coords" : [220,390]},
			"01.15" : { "name" : "1.18", "coords" : [195,245]},
			"01.18" : { "name" : "1.18", "coords" : [195,245]}
		},
		"restarea" : { "roomtypename" : "Rest Area"
		},
		"toilets" : { "roomtypename" : "Toilets"
		},
		"exits" : { "roomtypename" : "Exits"
		}
	},

	"2ndfloor" : { "floorname" : "2nd  Floor",
		"meetingroom" : { "roomtypename" : "Meeting Room",
			"02.08" : { "name" : "Old St.Melons", "coords" : [1015,370]},
			"02.09" : { "name" : "Pentwyn", "coords" : [940,370]},
			"02.10" : { "name" : "Radyr", "coords" : [850,370]},
			"02.11" : { "name" : "Riverside", "coords" : [750,370]},
			"02.12" : { "name" : "Roath", "coords" : [670,370]},
			"02.13" : { "name" : "Rumney", "coords" : [610,370]},
			"02.16" : { "name" : "Harvard", "coords" : [100,400]}
		},
		"teachingroom" : { "roomtypename" : "Teaching Room",
			"02.15" : { "name" : "02.19", "coords" : [190,400]},
			"02.18" : { "name" : "02.18", "coords" : [140,398]}
		},
		"itsuite" : { "roomtypename" : "IT Suite",
			"02.19" : { "name" : "02.19", "coords" : [60,250]}
		},
		"restarea" : { "roomtypename" : "Rest Area"
		},
		"toilets" : { "roomtypename" : "Toilets"
		},
		"exits" : { "roomtypename" : "Exits"
		}
	},

	"3rdfloor" : { "floorname" : "3rd Floor",
		"meetingroom" : { "roomtypename" : "Meeting Room",
			"03.06" : { "name" : "Trowbridge", "coords" : [1030,375]},
			"03.07" : { "name" : "Whitchurch", "coords" : [970,375]},
			"03.08" : { "name" : "Research Students", "coords" : [910,375]},
			"03.09" : { "name" : "Research Students", "coords" : [830,375]},
			"03.10" : { "name" : "Director of Research", "coords" : [740,375]},
			"03.17" : { "name" : "Enterprise Centre", "coords" : [540,400]},
			"03.18" : { "name" : "Enterprise Training Room", "coords" : [460,360]},
			"03.rsl" : { "name" : "Research Student Lounge", "coords" : [400,360]},
			"03.20" : { "name" : "Harvard Room", "coords" : [125,395]}
		},
		"toilets" : { "roomtypename" : "Toilets"
		},
		"exits" : { "roomtypename" : "Exits"
		}
	}

}

Open in new window


I am then creating the jquery to create the bullets:
	$.getJSON( "roomLocations2.json", function( data ) {

		$.each( data, function(floor, floorData ) {
			//create floor
			var htmlData = $("<ul>", { id: floor, class: "publicareasList floorGroup" });

			$.each( floorData, function(roomType, roomTypeData) {
				//create room type
				htmlData.append($('<li>', { class: 'floorItem', id: roomType }).append( $('<a>', { href: '#', 'data-type': roomType })
						.append($('<p>', { html: roomTypeData.roomtypename }))));

				/******$.each( roomTypeData, function(room, roomData) {
					//create room
					htmlData.append($('<ul>', { class: 'publicareasList typeGroup', id: roomTypeData.roomtypename })
						.append($('<li>', { class: 'typeItem' })
						.append($('<a>', { href: '#', 'data-type': roomTypeData.roomtypename, 'data-room': room })
						.append($('<p>', { html: roomData.name })))));
				});******/
			});

			if( floor == "groundfloor") htmlData.addClass('selectedFloor');
			$('#publicareasListWrapper').append(htmlData);
		});
	});

Open in new window




The issues I have are:
It fails when I uncomment the middle section:
Uncaught TypeError: Cannot use 'in' operator to search for '11' in Ground Floor
I think thats something to do with my json and the roomtypename in the wrong place.

Im also getting an extra blank li each time.
I also expect thats how im doing my json.

Any advice?

Steve
Steve TinsleyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
You need to test that you are looping over an object array before looping. That's not always the case with your JSON object. Some "roomTypeData" objects are simple strings like "Ground Floor". The easiest way to test is to see if an object property exists. If it does, you have an object array, go ahead and loop. Doing this will also eliminate the extra <li>s.
$.getJSON( "roomLocations2.json", function( data ) {

		$.each( data, function(floor, floorData ) {
			//create floor
			var htmlData = $("<ul>", { id: floor, class: "publicareasList floorGroup" });

			$.each( floorData, function(roomType, roomTypeData) { 
				//create room type
				if (roomTypeData.roomtypename) {
				    htmlData.append($('<li>', { class: 'floorItem', id: roomType }).append( $('<a>', { href: '#', 'data-type': roomType })
						.append($('<p>', { html: roomTypeData.roomtypename }))));
                
					$.each( roomTypeData, function(room, roomData) {
						//create room
						if (roomData.name) {
							htmlData.append($('<ul>', { class: 'publicareasList typeGroup', id: roomTypeData.roomtypename })
								.append($('<li>', { class: 'typeItem' })
								.append($('<a>', { href: '#', 'data-type': roomTypeData.roomtypename, 'data-room': room })
								.append($('<p>', { html: roomData.name })))));
						}
					});
				}
			});

			if( floor == "groundfloor") htmlData.addClass('selectedFloor');
			$('#publicareasListWrapper').append(htmlData);
		});
	});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve TinsleyAuthor Commented:
Thanks Tom, That solved that one!
Last problem... the hierarchy of the li's isn't quite right.

li output
The typeItem needs to be inside typeGroup.
Im sure its just how im doing my jquery...

Do you know how to fix?
0
Steve TinsleyAuthor Commented:
/*****Get Room Locations*****/
	var roomLocations = {};
	var readableRoomNames = {};

	$.getJSON( "roomLocations2.json", function( data ) {

		$.each( data, function(floor, floorData ) {
			//create floor
			var htmlData = $("<ul>", { id: floor, class: "publicareasList floorGroup" });

			$.each( floorData, function(roomType, roomTypeData) {
				//create room type
				if (roomTypeData.roomtypename) {
					htmlData.append($('<li>', { class: 'floorItem', id: roomType }).append( $('<a>', { href: '#', 'data-type': roomType })
							.append($('<p>', { html: roomTypeData.roomtypename }))));
					
					//if( rooms.length > 1) {
						htmlData.append($('<ul>', { class: 'publicareasList typeGroup' }));

					$.each( roomTypeData, function(room, roomData) {
						//create room
						if (roomData.name) {
							roomLocations[room] = roomData;
							
								htmlData.append($('<li>', { class: 'typeItem' })
								.append($('<a>', { href: '#', 'data-type': roomType, 'data-room': room })
								.append($('<p>', { html: roomData.name }))));
						}
					});
				}
			});

			if( floor == "groundfloor") htmlData.addClass('selectedFloor');
			$('#publicareasListWrapper').append(htmlData);
		});
	});

Open in new window

0
Tom BeckCommented:
How about this:
/*****Get Room Locations*****/
	var roomLocations = {};
	var readableRoomNames = {};

	$.getJSON( "js/roomlocations1.json", function( data ) {

		$.each( data, function(floor, floorData ) {
			//create floor
			var htmlData = $("<ul>", { id: floor, class: "publicareasList floorGroup" });

			$.each( floorData, function(roomType, roomTypeData) {
				//create room type
				if (roomTypeData.roomtypename) {
				    var roomGroup = $('<li>', { class: 'floorItem', id: roomType })
					        .append(( $('<a>', { href: '#', 'data-type': roomType })
							.append($('<p>', { html: roomTypeData.roomtypename }))));
					
					var typeGroup = $('<ul>', { class: 'publicareasList typeGroup' });

					$.each( roomTypeData, function(room, roomData) {
						//create room
						if (roomData.name) {
							roomLocations[room] = roomData;
							
								typeGroup.append($('<li>', { class: 'typeItem' })
								.append($('<a>', { href: '#', 'data-type': roomType, 'data-room': room })
								.append($('<p>', { html: roomData.name }))));
						}
					});
					if (typeGroup.children('li').length > 0) {
					   roomGroup.append(typeGroup);
					}
					htmlData.append(roomGroup);
				}
			});

			if( floor == "groundfloor") htmlData.addClass('selectedFloor');
			$('#publicareasListWrapper').append(htmlData);
		});
	});

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.