如何获取JSON对象中的键值? [英] How can I get the key value in a JSON object?

查看:52
本文介绍了如何获取JSON对象中的键值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JavaScript获取JSON对象中的键值和对象的长度?

How do I get the key value in a JSON object and the length of the object using JavaScript?

例如:

[
  {
    "amount": " 12185",
    "job": "GAPA",
    "month": "JANUARY",
    "year": "2010"
  },
  {
    "amount": "147421",
    "job": "GAPA",
    "month": "MAY",
    "year": "2010"
  },
  {
    "amount": "2347",
    "job": "GAPA",
    "month": "AUGUST",
    "year": "2010"
  }
]

这里,此数组的长度为3.为了得到,值([0].amount)很好,在索引[0]中,它具有三个名称值对.

Here, length of this array is 3. For getting, value is fine ([0].amount), in index[0] it has three name value pairs.

因此,我需要获取名称(例如数量或职位...总共四个名称),以及如何计算有多少个名称?

So, I need to get the name (like amount or job... totally four name) and also how do I count how many names there are?

推荐答案

首先,您没有在处理"JSON对象".您正在处理一个JavaScript对象. JSON是 text 表示法,但是如果您的示例代码有效([0].amount),则您已经将该表示法反序列化为JavaScript对象图. (您引用的内容根本不是有效的JSON;在 JSON 中,密钥必须用双引号引起来.您引用的是JavaScript对象文字,它是JSON的超集.)

First off, you're not dealing with a "JSON object." You're dealing with a JavaScript object. JSON is a textual notation, but if your example code works ([0].amount), you've already deserialized that notation into a JavaScript object graph. (What you've quoted isn't valid JSON at all; in JSON, the keys must be in double quotes. What you've quoted is a JavaScript object literal, which is a superset of JSON.)

这里,此数组的长度为2.

Here, length of this array is 2.

不,是3.

所以,我需要获取名字(例如数量或工作……总共四个名字),还要计算有多少个名字?

So, i need to get the name (like amount or job... totally four name) and also to count how many names are there?

如果您使用的是具有完全ECMAScript5支持的环境,则可以使用Object.keys( MDN )以将对象之一的可枚举键作为数组获取.如果不是这样(或者如果您只是想遍历它们而不是获取它们的数组),则可以使用for..in:

If you're using an environment that has full ECMAScript5 support, you can use Object.keys (spec | MDN) to get the enumerable keys for one of the objects as an array. If not (or if you just want to loop through them rather than getting an array of them), you can use for..in:

var entry;
var name;
entry = array[0];
for (name in entry) {
    // here, `name` will be "amount", "job", "month", then "year" (in no defined order)
}

完整的示例:

(function() {
  
  var array = [
    {
      amount: 12185,
      job: "GAPA",
      month: "JANUARY",
      year: "2010"
    },
    {
      amount: 147421,
      job: "GAPA",
      month: "MAY",
      year: "2010"
    },
    {
      amount: 2347,
      job: "GAPA",
      month: "AUGUST",
      year: "2010"
    }
  ];
  
  var entry;
  var name;
  var count;
  
  entry = array[0];
  
  display("Keys for entry 0:");
  count = 0;
  for (name in entry) {
    display(name);
    ++count;
  }
  display("Total enumerable keys: " + count);

  // === Basic utility functions
  
  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }
  
})();

因为您正在处理原始对象,所以上面的for..in循环很好(除非有人犯了用Object.prototype捣烂的罪过,但让我们假设不是).但是,如果您想要键的对象也可以从其原型继承可枚举的属性,则可以通过添加 own 键(而不是其原型的键) >打电话到那里:

Since you're dealing with raw objects, the above for..in loop is fine (unless someone has committed the sin of mucking about with Object.prototype, but let's assume not). But if the object you want the keys from may also inherit enumerable properties from its prototype, you can restrict the loop to only the object's own keys (and not the keys of its prototype) by adding a hasOwnProperty call in there:

for (name in entry) {
  if (entry.hasOwnProperty(name)) {
    display(name);
    ++count;
  }
}

这篇关于如何获取JSON对象中的键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆