Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何获取JSON文件中每个元素的值_Jquery_Json_Jsp - Fatal编程技术网

Jquery 如何获取JSON文件中每个元素的值

Jquery 如何获取JSON文件中每个元素的值,jquery,json,jsp,Jquery,Json,Jsp,我已经从servlet创建了它,并通过response.setContentType(“application/json”)传递它; Json文件: [ { "id": "1", "tsnm": "John1", "userId": "RoySoha", "date": "2020-04-22", "Button": "<button type='button' id='opener' cl

我已经从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> &nbsp&nbsp <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> &nbsp&nbsp <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> &nbsp&nbsp <button type='button' id='sure' class='btn btn-danger'><i class='fa fa-trash' aria-hidden='true'></i></button>"
      }
    ]
我需要ID、tsnm等的每个值。
您能帮助我吗?

您正在将
响应
对象放置在jQuery对象中,以便对其进行迭代。不要那样做。只需使用
forEach()
循环:

//模拟AJAX回调:
让响应=[
{“id”:“1”,“tsnm”:“John1”,“userId”:“RoySoha”,“日期”:“2020-04-22”,“按钮”:“},
{“id”:“2”,“tsnm”:“John2”,“userId”:“RoySoha”,“date”:“2020-04-22”,“Button”:“},
{“id”:“3”,“tsnm”:“John3”,“userId”:“RoySoha”,“日期”:“2020-04-22”,“按钮”:”
]
response.forEach(值=>{
var x1=value.id;
var x2=0.tsnm的值;
var x3=value.userId;
var x4=value.date;
var x5=value.Button;
控制台日志(x1、x2、x3、x4、x5);
});

如果JSON对象数组长度是固定的。您可以使用的

var数据=[{“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”:;

console.log(数据[0].id)它仍然向我显示“未定义”值所有变量。考虑到问题中的细节,我看不出这是怎么可能的。
console.log(response)
的输出是什么?未定义未定义。这是我得到的唯一东西。然后,
response
没有你期望的值。当我在控制台上打印响应值时,它看起来是这样的:[“{”id:“1”,“tsnm:“John1”,“userId:“RoySoha”,“date”:“RoySoha”,“date”…='fa trash'aria hidden='true'>“}”,“{”id:“2”,“tsnm:“John2”,“userId:“RoySoha”,“date”…='fa trash'aria hidden='true'>“}”,“{”id:“3”,“tsnm:“John3”,“userId:“RoySoha”,“date”…='fa fa trash'aria hidden='true'>“}”]
{"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> &nbsp&nbsp <button type='button' id='sure' class='btn btn-danger'><i class='fa fa-trash' aria-hidden='true'></i></button>"}
$('#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);
                //});
         });
         });