如何获取JSON对象中的键值?

时间:2022-01-03 23:50:11

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

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

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

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?

这里,这个数组的长度是3.为了获得值很好([0] .amount),在索引[0]中它有3个名称值对。所以,我需要得到这个名字(比如金额或工作......总共四个名字),还要计算有多少名字?

4 个解决方案

#1


13  

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.)

首先,你没有处理“JSON对象”。你正在处理一个JavaScript对象。 JSON是一种文本符号,但如果您的示例代码有效([0] .amount),那么您已经将该符号反序列化为JavaScript对象图。 (你引用的内容根本不是有效的JSON;在JSON中,键必须是双引号。你引用的是JavaScript对象文字,它是JSON的超集。)

Here, length of this array is 2.

这里,这个数组的长度是2。

No, it's 3.

不,这是3。

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

所以,我需要得到这个名字(比如金额或工作......总共四个名字),还要计算有多少名字?

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:

如果您使用的是具有完整ECMAScript5支持的环境,则可以使用Object.keys(spec | MDN)将其中一个对象的可枚举键作为数组获取。如果不是(或者如果你只想循环遍历它们而不是获取它们的数组),你可以使用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)
}

Full working example:

完整的工作示例:

(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);
  }
  
})();

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..in循环很好(除非有人犯了与Object.prototype混淆的罪,但我们假设没有)。但是,如果您想要键的对象也可以从其原型继承可枚举属性,则可以通过在其中添加hasOwnProperty调用将循环限制为仅对象自己的键(而不是其原型的键):

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

#2


16  

Json are basically represented as associate array in Javascript. Just need to loop over them to either read key or value

Json基本上表示为Javascript中的关联数组。只需要将它们循环到读取键或值即可

    var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 };

    //read key
    for (var key in JsonObj) {
       console.log(key);
       console.log(JsonObj[key]);
   }

#3


10  

You may need:

你可能需要:

Object.keys(JSON[0]);

To get something like:

得到类似的东西:

[ 'amount', 'job', 'month', 'year' ]

Note: Your JSON is invalid.

注意:您的JSON无效。

#4


0  

When you parse the JSON representation, it'll become a JavaScript array of objects.

解析JSON表示时,它将成为JavaScript对象数组。

Because of this, you can use the .length property of the JavaScript array to see how many elements are contained, and use a for loop to enumerate it.

因此,您可以使用JavaScript数组的.length属性来查看包含的元素数量,并使用for循环来枚举它。

#1


13  

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.)

首先,你没有处理“JSON对象”。你正在处理一个JavaScript对象。 JSON是一种文本符号,但如果您的示例代码有效([0] .amount),那么您已经将该符号反序列化为JavaScript对象图。 (你引用的内容根本不是有效的JSON;在JSON中,键必须是双引号。你引用的是JavaScript对象文字,它是JSON的超集。)

Here, length of this array is 2.

这里,这个数组的长度是2。

No, it's 3.

不,这是3。

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

所以,我需要得到这个名字(比如金额或工作......总共四个名字),还要计算有多少名字?

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:

如果您使用的是具有完整ECMAScript5支持的环境,则可以使用Object.keys(spec | MDN)将其中一个对象的可枚举键作为数组获取。如果不是(或者如果你只想循环遍历它们而不是获取它们的数组),你可以使用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)
}

Full working example:

完整的工作示例:

(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);
  }
  
})();

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..in循环很好(除非有人犯了与Object.prototype混淆的罪,但我们假设没有)。但是,如果您想要键的对象也可以从其原型继承可枚举属性,则可以通过在其中添加hasOwnProperty调用将循环限制为仅对象自己的键(而不是其原型的键):

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

#2


16  

Json are basically represented as associate array in Javascript. Just need to loop over them to either read key or value

Json基本上表示为Javascript中的关联数组。只需要将它们循环到读取键或值即可

    var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 };

    //read key
    for (var key in JsonObj) {
       console.log(key);
       console.log(JsonObj[key]);
   }

#3


10  

You may need:

你可能需要:

Object.keys(JSON[0]);

To get something like:

得到类似的东西:

[ 'amount', 'job', 'month', 'year' ]

Note: Your JSON is invalid.

注意:您的JSON无效。

#4


0  

When you parse the JSON representation, it'll become a JavaScript array of objects.

解析JSON表示时,它将成为JavaScript对象数组。

Because of this, you can use the .length property of the JavaScript array to see how many elements are contained, and use a for loop to enumerate it.

因此,您可以使用JavaScript数组的.length属性来查看包含的元素数量,并使用for循环来枚举它。