【问题标题】:Ajax retrieve value from json object ()Ajax 从 json 对象中检索值 ()
【发布时间】:2015-11-25 02:26:57
【问题描述】:

这是我的第一次 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 的新手。请不要标记为重复。]

【问题讨论】:

    标签: javascript jquery json ajax


    【解决方案1】:

    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>");
    })
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;

    试试这个方法。

    【讨论】:

    • 您可以通过更改名称来完成其余的工作
    • 感谢古拉迪奥。现在可以了。我是 ajax 和 JavaScript 的新手。但我很享受。
    • @user1365169 很高兴帮助 mate.happy 编码
    【解决方案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);
        }
    }
    

    ...或类似的东西。那应该生成无序列表。测试太累了,抱歉。

    【讨论】:

    • 我认为前面的答案更准确。你的也是作品。但是你不知道数组中的元素个数。
    猜你喜欢
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2013-10-16
    • 2017-08-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多