0

我的 javascript 有问题,我调用了一个返回此字符串的 ajax 方法:

{
  "ObjectResponse": {
    "Operation": "OK",
    "Response": "SUCCESS",
    "Message": "List of AAA Found",
    "List": [
      {
        "keySource": "gat\/images\/images_set\/apple.jpg",
        "idSiteKey": "1",
        "text": "Apple"
      },
      {
        "keySource": "gat\/images\/images_set\/cat.jpg",
        "idSiteKey": "2",
        "text": "Cat"
      },
      {
        "keySource": "gat\/images\/images_set\/coffee.jpg",
        "idSiteKey": "3",
        "text": "Coffee"
      },
      {
        "keySource": "gat\/images\/images_set\/dog.jpg",
        "idSiteKey": "4",
        "text": "Dog"
      },
      {
        "keySource": "gat\/images\/images_set\/horse.jpg",
        "idSiteKey": "5",
        "text": "Horse"
      },
      {
        "keySource": "gat\/images\/images_set\/police.jpg",
        "idSiteKey": "6",
        "text": "Police"
      },
      {
        "keySource": "gat\/images\/images_set\/tree.jpg",
        "idSiteKey": "7",
        "text": "Tree"
      }
    ]
  }
}

我以这种方式评估内容:

xhr.onreadystatechange = ensureReadiness; 
....
responseText = xhr.responseText;

如果我尝试在 javascript 上解析它:

response = JSON.parse(responseText);

如果我访问一个属性,response.ObjectResponse.Operation我确实得到了正确的内容..但是当我尝试访问列表时它总是刹车

如果我尝试相同的字符串,但不是调用服务,而是将内容分配给它可以工作的 var,我确实可以访问列表

var myTemporalString ='{"ObjectResponse":{"Operation":"OK","Response":"SUCCESS","Message":"List of Keys Found","List":...';
response.JSON.parse(myTemporalString);

任何建议为什么会发生这种情况?

4

2 回答 2

1

你可以试试这个方法

    xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {

                        try{
                                var mJsonData = JSON.parse(xhr.responseText);
                            }catch(err){
                                console.log(err);
                                alert(err);
                                return;
                            }

                        for(i=0;i<jsondata.ObjectResponse.List.length;i++){
                                   console.log(jsondata.ObjectResponse.List[i].text);
                                 console.log(jsondata.ObjectResponse.List[i].keySource);
                                   console.log(jsondata.ObjectResponse.List[i]. idSiteKey);
                           }

              }
           }
       }
于 2012-10-26T20:41:14.747 回答
0

使用循环!

var array = response.ObjectResponse.List;
var len = array.length;
for(i=0; i<len; i++) {
    //Use array[i] to access the list
    array[i].keySource
    array[i].idSiteKey
    array[i].text
}
于 2012-10-26T20:38:14.413 回答