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

缺口

我已经从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>"
      }
    ]

我的代码:现在,当我尝试访问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和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等的每个值。请您能帮我吗?

罗里·麦克罗森(Rory McCrossan)

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

// mock AJAX callback:
let response = [
  { "id": "1", "tsnm": "John1", "userId": "RoySoha", "date": "2020-04-22", "Button": " &nbsp&nbsp " }, 
  { "id": "2", "tsnm": "John2", "userId": "RoySoha", "date": "2020-04-22", "Button": " &nbsp&nbsp " }, 
  { "id": "3", "tsnm": "John3", "userId": "RoySoha", "date": "2020-04-22", "Button": " &nbsp&nbsp "}
]

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从json文件中获取每个值

如何使用org.json库从Java中的JSON文件获取每个键和值?

我如何获取此json中的值

如何从 SQL SERVER 中的 JSON 中获取每个对象的值

我如何在 json 文件中获取每个 json elemet 并将它们放入一个数组中

如何从ruby sinatra中的json获取每个值?

如何从我使用的输入值中获取数组的元素?

如何从 Python 中的 json 文件中获取某些元素?

我们可以遍历 event.target 并获取表单中每个元素的属性值吗

我如何使用jsonpath从json中获取多个元素?

如何从字典中的列表中获取所有值,我需要为每个项目获取标题

如何在我的 JSON 对象中获取相应的 JSON 值?

如何获取复杂 JSON 数据中的元素值

Swift 从 JSON 数组中的每个元素中获取元素

如何从我的 JSON 文件中获取值,在 python 中

我如何获取thymeleaf的html文件中的可选值?

如何从值获取json文件

如何从选择器中获取所有元素包含的属性的每个值?

如何在C#中获取多值xml文件中“ Ienumerable <String> Urls”中的每个值?

我如何使用Angularjs从json数据中获取特定值

我们如何才能将数组的每个元素彼此相乘并将每个乘积值存储在c ++中的数组中?

如何获取python中每个值的IQR

如何从 csv 文件中仅获取所选元素的值,但从不同的列中获取?

如何解析/替换 jq 中的值以获取 json 文件

如何从json文件中获取最高的3个值

如何在json文件中获取key的值数据

如何从网页获取JSON对象数组并将每个元素添加到HTML中的行?

获取数组值的每个元素

Python如何从列表中包含多个元素的json文件中获取元素