如何访问多维json对象

时间:2017-04-28 11:31:01

标签: jquery

我发送的json编码数组列表给出了以下输出:

[
  "2017",
  "4",
  "2017-04-28",
  "2017-4-30",
  "6",
  36,
  42,
  {
    "2017-4-1": "",
    "2017-4-2": "",
    "2017-4-3": 1,
    "2017-4-4": 1,
    "2017-4-5": "",
    "2017-4-6": "",
    "2017-4-7": "",
    "2017-4-8": "",
    "2017-4-9": "",
    "2017-4-10": "",
    "2017-4-11": "",
    "2017-4-12": "",
    "2017-4-13": 2,
    "2017-4-14": "",
    "2017-4-15": 1,
    "2017-4-16": 3,
    "2017-4-17": "",
    "2017-4-18": 1,
    "2017-4-19": "",
    "2017-4-20": "",
    "2017-4-21": "",
    "2017-4-22": "",
    "2017-4-23": "",
    "2017-4-24": "",
    "2017-4-25": "",
    "2017-4-26": 1,
    "2017-4-27": 2,
    "2017-4-28": "",
    "2017-4-29": "",
    "2017-4-30": 2
  }
]

我可以使用jquery访问上面的数组: -

var json = $.parseJSON(response);
var dateYear = json[0];

但是如何确定这些数据

"2017-4-1": "",
"2017-4-2": "",
"2017-4-3": 1,
"2017-4-4": 1,
"2017-4-5": "",
"2017-4-6": "",
"2017-4-7": "",
"2017-4-8": "",
"2017-4-9": "",
"2017-4-10": "",
"2017-4-11": "",
"2017-4-12": "",
"2017-4-13": 2,
"2017-4-14": "",
"2017-4-15": 1,
"2017-4-16": 3,
"2017-4-17": "",
"2017-4-18": 1,
"2017-4-19": "",
"2017-4-20": "",

3 个答案:

答案 0 :(得分:0)

要做到这一点,你只需要遍历数组的第8个元素,如下所示:



var dateYear = ["2017","4","2017-04-28","2017-4-30","6",36,42,{"2017-4-1":"","2017-4-2":"","2017-4-3":1,"2017-4-4":1,"2017-4-5":"","2017-4-6":"","2017-4-7":"","2017-4-8":"","2017-4-9":"","2017-4-10":"","2017-4-11":"","2017-4-12":"","2017-4-13":2,"2017-4-14":"","2017-4-15":1,"2017-4-16":3,"2017-4-17":"","2017-4-18":1,"2017-4-19":"","2017-4-20":"","2017-4-21":"","2017-4-22":"","2017-4-23":"","2017-4-24":"","2017-4-25":"","2017-4-26":1,"2017-4-27":2,"2017-4-28":"","2017-4-29":"","2017-4-30":2}];

for (var key in dateYear[7]) {
  console.log(key + ' = ' + dateYear[7][key]);
}




答案 1 :(得分:0)

您可以访问以下对象中的密钥:for(key in obj)



var obj = [
  "2017",
  "4",
  "2017-04-28",
  "2017-4-30",
  "6",
  36,
  42,
  {
    "2017-4-1": "",
    "2017-4-2": "",
    "2017-4-3": 1,
    "2017-4-4": 1,
    "2017-4-5": "",
    "2017-4-6": "",
    "2017-4-7": "",
    "2017-4-8": "",
    "2017-4-9": "",
    "2017-4-10": "",
    "2017-4-11": "",
    "2017-4-12": "",
    "2017-4-13": 2,
    "2017-4-14": "",
    "2017-4-15": 1,
    "2017-4-16": 3,
    "2017-4-17": "",
    "2017-4-18": 1,
    "2017-4-19": "",
    "2017-4-20": "",
    "2017-4-21": "",
    "2017-4-22": "",
    "2017-4-23": "",
    "2017-4-24": "",
    "2017-4-25": "",
    "2017-4-26": 1,
    "2017-4-27": 2,
    "2017-4-28": "",
    "2017-4-29": "",
    "2017-4-30": 2
  }
];

var last = obj[7];
for(key in last) {
  console.log(key);
}




答案 2 :(得分:0)

使用以下jQuery代码。运行一个 for() 循环来获取所有数组数据,然后检查对象,然后再添加一个子for()循环来访问另一个子数据在对象类型中。

检查以下代码段:

for (var i = 0; i < myArr.length; i++) {
        if (typeof myArr[i] === "object") {
          for (var sec = 1; sec < 30; sec++) {
            $("ul").append("<li><span>" + myArr[i]["2017-4-" + sec] + "</span></li>")
          }
        } else {
          $("ul").append("<li>" + myArr[i] + "</li>")
        }

      }

&#13;
&#13;
$(document).ready(function() {
  var myArr = [
    "2017",
    "4",
    "2017-04-28",
    "2017-4-30",
    "6",
    36,
    42,
    {
      "2017-4-1": "object 1",
      "2017-4-2": "object 2 ",
      "2017-4-3": 1,
      "2017-4-4": 1,
      "2017-4-5": "",
      "2017-4-6": "",
      "2017-4-7": "",
      "2017-4-8": "",
      "2017-4-9": "",
      "2017-4-10": "",
      "2017-4-11": "",
      "2017-4-12": "",
      "2017-4-13": 2,
      "2017-4-14": "",
      "2017-4-15": 1,
      "2017-4-16": 3,
      "2017-4-17": "",
      "2017-4-18": 1,
      "2017-4-19": "",
      "2017-4-20": "",
      "2017-4-21": "",
      "2017-4-22": "",
      "2017-4-23": "",
      "2017-4-24": "",
      "2017-4-25": "",
      "2017-4-26": 1,
      "2017-4-27": 2,
      "2017-4-28": "",
      "2017-4-29": "",
      "2017-4-30": 2
    }
  ]

  // Fetching data
  for (var i = 0; i < myArr.length; i++) {
    if (typeof myArr[i] === "object") {
      for (var sec = 1; sec < 30; sec++) {
        $("ul").append("<li><span>" + myArr[i]["2017-4-" + sec] + "</span></li>")
      }
    } else {
      $("ul").append("<li>" + myArr[i] + "</li>")
    }

  }
});
&#13;
li span {
  color: green
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul></ul>
&#13;
&#13;
&#13;