1
{
   "id":"1",
   "firstName":"vishal",
   "姓氏":"gehlot",
   "title":"先生",
   "officePhone":"643636",
   “最后修改”:””
},
{
   "id":"2",
   "firstName":"daya",
   "lastName":"dayaji",
   "title":"先生",
   "officePhone":"858587",
   “最后修改”:””
},
{
   "id":"7",
   "firstName":"tripti",
   “姓氏”:“三”,
   “标题”:“女士”,
   "officePhone":"4535",
   “最后修改”:””
},
{
   “id”:“59”,
   "firstName":"大雅",
   “姓氏”,
   “标题”:””,
   "officePhone":"698789",
   “最后修改”:””
},
{
   “身份证”:“89”,
   "firstName":"prashant",
   "姓氏":"p",
   “标题”:””,
   "officePhone":"987698",
   “最后修改”:””
}

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

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

4

3 回答 3

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/

于 2012-06-26T06:36:34.830 回答
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循环读取它的每个属性。

于 2012-06-26T06:31:48.540 回答
0

那是一个javascript对象。假设您将其保存在变量中

var myObjects = {...}

使用简单的“点”符号访问它

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

编辑:抱歉,要获取每个属性:

尝试

for (var propertyName in myObject) {
     alert(myObject[propertyName]);
}
于 2012-06-26T06:32:39.523 回答