我已经从servlet创建了它,并通过response.setContentType(“ application / json”); Json文件:
[
{
"id": "1",
"tsnm": "John1",
"userId": "RoySoha",
"date": "2020-04-22",
"Button": "<button type='button' id='opener' class='btn btn-warning'><i class='fa fa-pencil' aria-hidden='true'></i></button>    <button type='button' id='sure' class='btn btn-danger'><i class='fa fa-trash' aria-hidden='true'></i></button>"
},
{
"id": "2",
"tsnm": "John2",
"userId": "RoySoha",
"date": "2020-04-22",
"Button": "<button type='button' id='opener' class='btn btn-warning'><i class='fa fa-pencil' aria-hidden='true'></i></button>    <button type='button' id='sure' class='btn btn-danger'><i class='fa fa-trash' aria-hidden='true'></i></button>"
},
{
"id": "3",
"tsnm": "John3",
"userId": "RoySoha",
"date": "2020-04-22",
"Button": "<button type='button' id='opener' class='btn btn-warning'><i class='fa fa-pencil' aria-hidden='true'></i></button>    <button type='button' id='sure' class='btn btn-danger'><i class='fa fa-trash' aria-hidden='true'></i></button>"
}
]
我的代码:现在,当我尝试访问JSON时,它仅返回每个数组值,例如
{"id":"1","tsnm":"John1","userId":"RoySoha","date":"2020-04-22","Button":"<button type='button' id='opener' class='btn btn-warning'><i class='fa fa-pencil' aria-hidden='true'></i></button>    <button type='button' id='sure' class='btn btn-danger'><i class='fa fa-trash' aria-hidden='true'></i></button>"}
所以现在,我如何才能分别获取ID和tsnm值?“ id” = 1,“ tsnm” =“ John1”等
$('#apps').change(function(event) {
var id = $("select#apps").val();
$.get('PopulateTableController', {
ApplicationId : id
}, function(response) {
console.log(response);
//$.getJSON(response, function(data){
alert("from json");
var ed = '';
//data = JSON.parse(response);
//console.log(data);
$(response).each(function(key, value){
console.log(value)
var x1 = value.id;var x2 = value.tsnm;var x3 = value.userId;var x4 = value.date;var x5 = value.Button;
console.log(x1);
});
$('.tab').append(ed);
console.log(ed);
//});
});
});
我需要ID,tsnm等的每个值。请您能帮我吗?
您将response
对象放置在jQuery对象中以便对其进行迭代。不要那样做 只需使用一个forEach()
循环:
// mock AJAX callback:
let response = [
{ "id": "1", "tsnm": "John1", "userId": "RoySoha", "date": "2020-04-22", "Button": "    " },
{ "id": "2", "tsnm": "John2", "userId": "RoySoha", "date": "2020-04-22", "Button": "    " },
{ "id": "3", "tsnm": "John3", "userId": "RoySoha", "date": "2020-04-22", "Button": "    "}
]
response.forEach(value => {
var x1 = value.id;
var x2 = value.tsnm;
var x3 = value.userId;
var x4 = value.date;
var x5 = value.Button;
console.log(x1, x2, x3, x4, x5);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
根据以下评论进行更新
当我在控制台上打印响应值时,它看起来像这样:
[“ {” id“:” 1“,” tsnm“:” John1“,” userId“:” RoySoha“,” date“…='fa fa -trash'aria-hidden ='true'>“}”,“ {” id“:” 2“,” tsnm“:” John2“,” userId“:” RoySoha“,” date“…='fa fa- trash'aria-hidden ='true'>“}”,“ {” id“:” 3“,” tsnm“:” John3“,” userId“:” RoySoha“,” date“…='fa fa-trash 'aria-hidden ='true'>“}”]
微小但非常重要的区别是响应是一个包含多个JSON编码字符串的数组。在使用它们之前,您需要对其进行解码。尝试这个:
// mock AJAX callback:
let response = [
'{"id":"1","tsnm":"John1","userId":"RoySoha","date":"2020-04-22","Button":"your HTML..."}',
'{"id":"2","tsnm":"John2","userId":"RoySoha","date":"2020-04-22","Button":"your HTML..."}',
'{"id":"3","tsnm":"John3","userId":"RoySoha","date":"2020-04-22","Button":"your HTML..."}'
]
response.forEach(json => {
let value = JSON.parse(json);
var x1 = value.id;
var x2 = value.tsnm;
var x3 = value.userId;
var x4 = value.date;
var x5 = value.Button;
console.log(x1, x2, x3, x4, x5);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句