【问题标题】:how get each value from json file如何从json文件中获取每个值
【发布时间】:2016-08-05 14:54:17
【问题描述】:

我尝试将数据发送到 json 文件中的数据表,如下所示: 我的 json 文件的示例数据:

"responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "q":"vba",
      "indent":"true",
      "fl":"name,role_t,born,natio_t",
      "wt":"json"}},
  "response":{"numFound":7,"start":0,"docs":[
      {
        "name":"Khouli",
        "born":["1978-04-03T00:00:00Z"],
        "natio_t":"tunisien",
        "role_t":"Consultant"},
      {
        "name":"Atayi",
        "born":["1987-06-24T00:00:00Z"],
        "natio_t":"Francaise",
        "role_t":"Consultant"}
}

这是我的功能:

$ ( document ).ready(function() {
$.ajax({
    type: 'GET',
    url: '../search.json',
    success: function(data) {
        $.each(data, function(i, data) {
            var body = "<tr>";
            body    += "<td>" + data.name + "</td>";
            body    += "<td>" + data.born + "</td>";
            body    += "<td>" + data.natio_t + "</td>";
            body    += "<td>" + data.role_t + "</td>";

            body    += "</tr>";
            $('.datatable-ajax-source table').append(body);
        });

但我得到了价值不定的桌子

如何从 json 文件中获取该值

【问题讨论】:

  • 您的 JSON 数据以不同的方式无效。请发布有效的 JSON。
  • 显然data 不是一个数组。为什么你会把它当作一个超出我的范围。

标签: javascript json datatables cakephp-2.6


【解决方案1】:

您的输入数据似乎有误:在此处更正数据:

{
"responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "q":"vba",
      "indent":"true",
      "fl":"name,role_t,born,natio_t",
      "wt":"json"}},
      "response":{"numFound":7,"start":0,"docs":[
      {
        "name":"Khouli",
        "born":["1978-04-03T00:00:00Z"],
        "natio_t":"tunisien",
        "role_t":"Consultant"},
      {
        "name":"Atayi",
        "born":["1987-06-24T00:00:00Z"],
        "natio_t":"Francaise",
        "role_t":"Consultant"}
     ]
}
}

处理这个问题的正确 JS:

$(document).ready(function() {
$.ajax({
type: 'GET',
url: '../search.json',
success: function(data) {
    // Data is the root node of your json response.
    // You need to navigate through the "docs" node.
    if(data.response.docs && data.response.docs.length > 0)
    {
    $.each(data.response.docs, function(i, item) {
        var body = "<tr>";

        body    += "<td>" + item.name + "</td>";
        body    += "<td>" + item.born + "</td>";
        body    += "<td>" + item.natio_t + "</td>";
        body    += "<td>" + item.role_t + "</td>";

        body    += "</tr>";
        $('.datatable-ajax-source table').append(body);
    }
    });

您没有得到正确的 data 值。 你应该得到docs.data[i],在这种情况下,item.each() 中定义。

【讨论】:

  • 谢谢@DidierAupest 你的回答,因为但是当我尝试这个解决方案时,我得到这个错误 jquery.min.js:4 Uncaught TypeError: Cannot read property 'length' of undefinedeach @ jquery.min.js :4success@application.js:111c@jquery.min.js:4fireWith@jquery.min.js:4k@jquery.min.js:6r@jquery.min.js:6
  • 还是一样的错误 application.js:116 Uncaught TypeError: Cannot read property 'length' of undefined in this ligne if(data.response.docs && data.docs.length > 0)跨度>
  • 我修复了它,谢谢您的帮助:我只是启发或您的建议,我将 data.docs 替换为 data.response.docs 并且它现在可以工作了。
  • 我忘了在任何地方都应用这个更改!
【解决方案2】:

你没有告诉 jquery 你期待 json 返回,所以它接受来自服务器响应的 JSON 字符串作为纯文本,并将它作为纯文本传递给 data

要么使用.getJSON(),它会将响应自动解析为原生JS数据结构,要么

$.ajax(
  ...
   dataType: "json"
  ...
);

【讨论】:

  • 确实这可能是问题的一部分,但如果服务器正确设置了 Content-Type 标头,jQuery 应该解析它。无论哪种方式,据我所知,他需要更深入地遍历结构,目前是无效的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多