Ajax从json对象()中检索值
Ajax retrieve value from json object ()
这是我的第一次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的新手。请不要标记为重复。]
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>
试试这个方法。
这样做:
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);
}
}
或者类似的东西。这应该会生成未排序的列表。对不起,我试得太累了。
相关文章:
- 可以't使用Angular解析/检索JSON
- 在AngularJS中使用$http.get检索JSON时出现错误400
- 在 ajax 成功和循环 json 数组时检索 json 数组
- 检索JSON数据并使用AJAX在表中显示
- gmaps4rails:如何从控制器中检索json值
- 在不知道名称的情况下检索json元素
- JSon挑战:如何在JqxGrid中检索JSon编码的值
- AngularJS基于日期检索JSON记录
- Angular JS:根据 id 检索 json 数据
- 仅使用该值检索 JSON 密钥
- 无法在 JavaScript 中检索 JSON 值
- 使用 Ember Data 检索 json 文件
- jQuery ajax 无法检索 json
- 如何从PHP文件中检索JSON数据
- 使用 jquery ajax 跨域检索 json 数据
- 从 youtube 网址检索 JSON
- 如何使用 JavaScript 检索 JSON 对象数组值
- JQuery AJAX-如何从URL检索JSON数据
- 使用 .getJSON 从 php 文件中检索 JSON 数据
- 正在检索Json字符串的一部分