1
{
   "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

1
  • You have an array of json objects, but the array brakes, [...], are missing (at least in what you have posted). Commented Jun 26, 2012 at 6:34

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Normal for loop is used for arrays not objects :)
@Blaster If you examine the data, you'd see it is an array, with each element being a json object, so sandeep's code would work, but yours would not.
@sandeeppatel Check my code ..when i m putting data manually it is working but not working with ajax response
@sandeeppatel Thanks it worked for me....the mistake i did was ..after ajax response i need to do eval with ajax resonse var obj = eval('(' + xmlhttp.responseText + ')');
1

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.

Comments

0

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]);
}

1 Comment

How to fetch each data using javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.