Reputation: 437
{ "id":"1", "firstName":"vishal", "lastName":"gehlot", "title":"Mr.", "officePhone":"643636", "lastModified":"" }, { "id":"2", "firstName":"daya", "lastName":"dayaji", "title":"Mr.", "officePhone":"858587", "lastModified":"" }, { "id":"7", "firstName":"tripti", "lastName":"tri", "title":"ms", "officePhone":"4535", "lastModified":"" }, { "id":"59", "firstName":"Daya", "lastName":"s", "title":"", "officePhone":"698789", "lastModified":"" }, { "id":"89", "firstName":"prashant", "lastName":"p", "title":"", "officePhone":"987698", "lastModified":"" }
How to fetch each data using JavaScript?
<script type="text/javascript" src="ajax.js">
function loadXMLDoc()
{
alert('inside');
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
// alert(xmlhttp.readyState);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// alert(xmlhttp.responseText);
var obj = [{"id":"1","firstName":"vishal","lastName":"gehlot","title":"Mr.","officePhone":"643636","lastModified":""},{"id":"2","firstName":"daya","lastName":"dayaji","title":"Mr.","officePhone":"858587","lastModified":""},{"id":"7","firstName":"tripti","lastName":"tri","title":"ms","officePhone":"4535","lastModified":""},{"id":"59","firstName":"Daya","lastName":"s","title":"","officePhone":"698789","lastModified":""},{"id":"89","firstName":"prashant","lastName":"p","title":"","officePhone":"987698","lastModified":""}];
alert(obj);
for(var index=0; index<obj.length;index++) {
alert((obj[index].id));
alert((obj[index].firstName));
//like this....
}
}
}
xmlhttp.open("GET","localapi.php?mode=list",true);
xmlhttp.send();
}
</script>
<input type="button" value="Check Data" onclick="loadXMLDoc()" />
this code is working fro me but when same response which i have taken in obj getting from ajax it is not working ...what will be the issue
Upvotes: 1
Views: 6217
Reputation: 5131
That's a javascript object. lets say you saved it in a variable
var myObjects = {...}
Access it with the simple "dot" notation
alert(myObject.id)
produces an alert "1"
alert(myObject.firstName)
produces "vishal"
Edit: sorry, to get each property:
try
for (var propertyName in myObject) {
alert(myObject[propertyName]);
}
Upvotes: 0
Reputation: 436
code:-
var obj=[{"id":"1","firstName":"vishal","lastName":"gehlot","title":"Mr.","officePhone":"643636","lastModified":""},{"id":"2","firstName":"daya","lastName":"dayaji","title":"Mr.","officePhone":"858587","lastModified":""},{"id":"7","firstName":"tripti","lastName":"tri","title":"ms","officePhone":"4535","lastModified":""},{"id":"59","firstName":"Daya","lastName":"s","title":"","officePhone":"698789","lastModified":""},{"id":"89","firstName":"prashant","lastName":"p","title":"","officePhone":"987698","lastModified":""}];
for(var index=0; index<obj.length;index++) {
document.write(obj[index].id)
document.write(obj[index].firstName)
//like this....
}
link:- http://jsfiddle.net/rcDue/3/
Upvotes: 3
Reputation: 9110
How to fetch each data using javascript
Like this:
var obj = JSON.parse(yourJSONData);
for (var x in obj) {
if (obj.hasOwnProperty(x)) {
console.log(obj[x]);
}
}
The above code uses JSON.parse
to convert your JSON string to JavaScript object and then uses for-in
loop to read each property of it.
Upvotes: 1