StealthRT StealthRT - 1 year ago 72
Ajax Question

AJAX jquery returned json data looping key and value

Hey all I currently have the following JSON returned when calling my ajax coldfusion .cfc page:

"[{\"USERA\": \"LiveP\", \"STATE\": \"None Given\", \"ROLES\": \"District Administrator<br/>Personnel Admin<br/>**** Coordinator\", \"ROLEASSIGNED\": \"LG AdminPersonnel System\", \"ADDRESS\": \"None Given\", \"UPDATEURL\": \"/personnel/search_ajax.cfc?func=edit=2*******\", \"TELEPHONE\": \"None Given\", \"ADDRESS2\": \"None Given\", \"ZIPCODE\": \"None Given\", \"LOCATION\": \"<a href=\\\"locations.cfm?func=view&locationID=\\\"2*******\\\">Demo New School (Primary)</a>\", \"SYSTEMID\": 87024, \"HOMETELEPHONE\": \"None Given\", \"MANAGEURL\": \"tools.cfm?userID=2*******\", \"MERGEURL\": \"/personnel/search_ajax.cfc?func=merge&userID=2*******\", \"EMAIL\": \"\", \"SUBJECTTAUGHT\": \"None Given\", \"CITY\": \"None Given\", \"POSITION\": \"None Given\"}]"

When I run this code below it gives me the above JSON:

success: function(data) {
var sData = JSON.stringify(data);

Now if I do not use JSON.stringify then my output is:


enter image description here

What I am ultimatlly looking to do is loop through this returned JSON and get the key and value without needing to know the key (a.k.a. sData.Address, sData.Address2, sData.City, etc etc).

I plan on putting it in this type of format:

var theHTML = "";

theHTML += "<tr><td>" + key + "</td><td>" + value + "</td></tr>";

Which that only returns:

<tr><td>0</td><td>[object Object]</td></tr>

I'm sure I'm just missing something little but I just can't find what that is.

Answer Source

I dont have the reputation to comment but it looks like your data is in an array and you want to loop over the first object in the array. You could try this.

var theHTML = "";

   theHTML += "<tr><td>" + key + "</td><td>" + value + "</td></tr>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download