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

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

问题描述

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

例如:

How to get the key valu in JSON object and length of the object using JavaScript.
For Example:

[
  {
    "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] 它有3个名字值对。所以,我需要得到这个名字(比如金额或工作......总共四个名字)而且还要计算如何有很多名字吗?

Here, length of this array is 3. For getting value is fine ([0].amount), in index[0] it has 3 name value pair.
So, I need to get the name (like amount or job... totally four name) and also to count how many names are there?

推荐答案

首先,你没有处理JSON对象。你正在处理一个JavaScript对象。 JSON是文本表示法,但如果您的示例代码有效( [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。 / p>

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 spec | 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 ,但我们假设没有)。但是,如果您想要键的对象也可以从其原型继承可枚举属性,则可以通过添加<$将循环限制为仅对象的自己的键(而不是其原型的键)。 c $ c> hasOwnProperty 在那里打电话:

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天全站免登陆