从JavaScript获取来自AJAX(JSON)响应的数据

时间:2012-06-26 06:28:56

标签: javascript ajax json

{
   "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":""
}

如何使用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()" />

这段代码对我有用,但是当我从obj获取相同的响应时,它不起作用......将会是什么问题

3 个答案:

答案 0 :(得分:3)

代码: -

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....
            }

链路: - http://jsfiddle.net/rcDue/3/

答案 1 :(得分:2)

  

如何使用javascript

获取每个数据

像这样:

var obj = JSON.parse(yourJSONData);

for (var x in obj) {
  if (obj.hasOwnProperty(x)) {
    console.log(obj[x]);
  }
}

上面的代码使用JSON.parse将您的JSON字符串转换为JavaScript对象,然后使用for-in循环来读取它的每个属性。

答案 2 :(得分:0)

这是一个javascript对象。假设你把它保存在一个变量

var myObjects = {...}

使用简单的“点”表示法访问它

alert(myObject.id)生成警告“1” alert(myObject.firstName)产生“vishal”

编辑:对不起,要获得每个房产:

for (var propertyName in myObject) {
     alert(myObject[propertyName]);
}