Ajax从json对象中检索值()

时间:2015-11-25 02:26:57

标签: javascript jquery json ajax

这是我的第一次ajax体验。我已经使用ajax从服务器成功获得了GET响应。但是我遇到了从JSON中检索值的问题。

我的ajax电话:

url: "ur",
type: "GET",
cache: false,    
dataType:"json",  
contentType: "application/json",
success: function(res) {                        
    console.log(res);
}

来自服务器的JSON响应:

{
  "message": [
    {
      "files": "SS", 
      "messageBody": "gOOD", 
      "messageID": 1, 
      "messageTitle": "THTH", 
      "photos": "DD", 
      "userID": 2, 
      "videos": "FF"
    }, 
    {
      "files": "", 
      "messageBody": "bla bala blaa", 
      "messageID": 2, 
      "messageTitle": "another", 
      "photos": "", 
      "userID": 3, 
      "videos": ""
    }, 
    {
      "files": "The Cock files", 
      "messageBody": "New message 11 Body", 
      "messageID": 3, 
      "messageTitle": "New message 11 Title", 
      "photos": "The Cock photos", 
      "userID": 3, 
      "videos": "The Cock videos"
    }
  ]
}

我可以在控制台中打印JSON对象。但问题是从JSON中检索内容。我已经尝试过多种方法从JSON对象中检索值。任何帮助都会很明显。

[注意:我在Ajax中非常非常新。请不要标记为重复。]

2 个答案:

答案 0 :(得分:3)

var res= {
  "message": [
    {
      "files": "SS", 
      "messageBody": "gOOD", 
      "messageID": 1, 
      "messageTitle": "THTH", 
      "photos": "DD", 
      "userID": 2, 
      "videos": "FF"
    }, 
    {
      "files": "", 
      "messageBody": "bla bala blaa", 
      "messageID": 2, 
      "messageTitle": "another", 
      "photos": "", 
      "userID": 3, 
      "videos": ""
    }, 
    {
      "files": "The Cock files", 
      "messageBody": "New message 11 Body", 
      "messageID": 3, 
      "messageTitle": "New message 11 Title", 
      "photos": "The Cock photos", 
      "userID": 3, 
      "videos": "The Cock videos"
    }
  ]
}




$.each(res.message, function(index,value){
console.log(value.files)
console.log(value.messageBody)
console.log(value.messageID)

$('body').append(value.files +"<br>" + value.messageBody +"<br>" + value.messageID +"<hr>");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

以这种方式试试。

答案 1 :(得分:2)

这样做:

var x = {
  "message": [
    {
      "files": "SS", 
      "messageBody": "gOOD", 
      "messageID": 1, 
      "messageTitle": "THTH", 
      "photos": "DD", 
      "userID": 2, 
      "videos": "FF"
    }, 
    {
      "files": "", 
      "messageBody": "bla bala blaa", 
      "messageID": 2, 
      "messageTitle": "another", 
      "photos": "", 
      "userID": 3, 
      "videos": ""
    }, 
    {
      "files": "The Cock files", 
      "messageBody": "New message 11 Body", 
      "messageID": 3, 
      "messageTitle": "New message 11 Title", 
      "photos": "The Cock photos", 
      "userID": 3, 
      "videos": "The Cock videos"
    }
  ]
};

然后您访问您的数据,在这种情况下,如下所示:

var z = x.message[0].files; // value of z is "SS"

补充阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

更新: 在javascript中,通过检查其长度来获取数组中的元素数,就像在这种情况下:     x.message.length;

因此,如果您想列出所有数据,可以执行以下操作:

for(var i = 0; i < x.message.length; i++) {
    var tempUl = document.createElement('ul');
    tempDiv.id = "object"+i;

    // now you can do something like this
    var tempLi = document.createElement('li');
    tempLi.innerHTML = x.message[i].files;   // or x.message[i]['files'];
    tempUl.appendChild(tempLi);
    // ... and so one ... 

    // or you can do it like this
    for(var j = 0; j < Object.keys(x.message[i]).length; j++) {
        var tempLi = document.createElement('li');
        var tempVal = Object.keys(x.message[i])[j];

        tempLi.className = tempVal;    // if you need to sort it or access them by any other way... also you can use classList.add();
        tempLi.innerHTML = x.message[i][tempVal];
        tempUl.appendChild(tempLi);
    }
}

......或类似的东西。这应该生成无序列表。我很累,测试它,抱歉。