如何使用JavaScript获取json数据中的数组名称

时间:2022-01-04 09:47:21

I have a JSON object which comes back like this from a JavaScript API call:

我有一个JSON对象,它从JavaScript API调用返回:

{
  "myArray": [
    {
      "version": 5,
      "permissionMask": 1
    },
    {
      "version": 126,
      "permissionMask": 1
    }
  ]
}

How can I access the name of the array (i.e myArray) in JavaScript. I need to use the name of the array to determine the flow later on.

如何在JavaScript中访问数组的名称(即myArray)。我需要使用数组的名称来确定以后的流程。

3 个解决方案

#1


3  

Object.keys(data)[0]
# => "myArray"

A terminology note: This solution assumes you have a JavaScript object. You might have a JSON string, in which case this is the solution:

术语说明:此解决方案假定您有一个JavaScript对象。您可能有一个JSON字符串,在这种情况下,这是解决方案:

Object.keys(JSON.parse(data))[0]
# => "myArray"

However, "JSON object", in JavaScript, is just one - the one I used just now, that has JSON.parse and JSON.stringify methods. What you have is not a JSON object except perhaps in a trivial interpretation of the second case, where all values in JavaScript are objects, including strings.

但是,JavaScript中的“JSON对象”只是一个 - 我刚才使用的那个,它有JSON.parse和JSON.stringify方法。你所拥有的不是JSON对象,除非是对第二种情况的简单解释,其中JavaScript中的所有值都是对象,包括字符串。

#2


2  

Use getOwnPropertyNames to get a list of the properties of the object in array form.

使用getOwnPropertyNames以数组形式获取对象的属性列表。

Example:

例:

var myObj = {
  "myArray": [
    {
      "version": 5,
      "permissionMask": 1

    },
    {
      "version": 126,
      "permissionMask": 1

    }
  ]
},
names = Object.getOwnPropertyNames(myObj);
alert(names[0]); // alerts "myArray"

Note: If the object can have more than one property, like myArray, myInt, and myOtherArray, then you will need to loop over the results of getOwnPropertyNames. You would also need to do type-testing, as in if(names[0] instanceof Array) {...} to check the property type. Based on your example in your question, I have not fleshed all of that out here.

注意:如果对象可以有多个属性,例如myArray,myInt和myOtherArray,那么您将需要遍历getOwnPropertyNames的结果。您还需要进行类型测试,如if(names [0] instanceof Array){...}来检查属性类型。根据你问题中的例子,我没有在这里充实。

#3


0  

The other answers are good if you have no control over the return format.

如果您无法控制返回格式,其他答案都很好。

However, if you can, I'd recommend changing the return format to put the important values you care about as actual values instead of keys to make it clearer. For example, something like this:

但是,如果可以,我建议更改返回格式,将您关心的重要值作为实际值而不是键,以使其更清晰。例如,像这样:

result = 
{
  "name: "myArray",
  "value": [
    {
      "version": 5,
      "permissionMask": 1
    },
    {
      "version": 126,
      "permissionMask": 1
    }
  ]
}

Then, it's a lot clearer to reliably access the property you care about: result.name

然后,可靠地访问您关心的属性更清楚:result.name

#1


3  

Object.keys(data)[0]
# => "myArray"

A terminology note: This solution assumes you have a JavaScript object. You might have a JSON string, in which case this is the solution:

术语说明:此解决方案假定您有一个JavaScript对象。您可能有一个JSON字符串,在这种情况下,这是解决方案:

Object.keys(JSON.parse(data))[0]
# => "myArray"

However, "JSON object", in JavaScript, is just one - the one I used just now, that has JSON.parse and JSON.stringify methods. What you have is not a JSON object except perhaps in a trivial interpretation of the second case, where all values in JavaScript are objects, including strings.

但是,JavaScript中的“JSON对象”只是一个 - 我刚才使用的那个,它有JSON.parse和JSON.stringify方法。你所拥有的不是JSON对象,除非是对第二种情况的简单解释,其中JavaScript中的所有值都是对象,包括字符串。

#2


2  

Use getOwnPropertyNames to get a list of the properties of the object in array form.

使用getOwnPropertyNames以数组形式获取对象的属性列表。

Example:

例:

var myObj = {
  "myArray": [
    {
      "version": 5,
      "permissionMask": 1

    },
    {
      "version": 126,
      "permissionMask": 1

    }
  ]
},
names = Object.getOwnPropertyNames(myObj);
alert(names[0]); // alerts "myArray"

Note: If the object can have more than one property, like myArray, myInt, and myOtherArray, then you will need to loop over the results of getOwnPropertyNames. You would also need to do type-testing, as in if(names[0] instanceof Array) {...} to check the property type. Based on your example in your question, I have not fleshed all of that out here.

注意:如果对象可以有多个属性,例如myArray,myInt和myOtherArray,那么您将需要遍历getOwnPropertyNames的结果。您还需要进行类型测试,如if(names [0] instanceof Array){...}来检查属性类型。根据你问题中的例子,我没有在这里充实。

#3


0  

The other answers are good if you have no control over the return format.

如果您无法控制返回格式,其他答案都很好。

However, if you can, I'd recommend changing the return format to put the important values you care about as actual values instead of keys to make it clearer. For example, something like this:

但是,如果可以,我建议更改返回格式,将您关心的重要值作为实际值而不是键,以使其更清晰。例如,像这样:

result = 
{
  "name: "myArray",
  "value": [
    {
      "version": 5,
      "permissionMask": 1
    },
    {
      "version": 126,
      "permissionMask": 1
    }
  ]
}

Then, it's a lot clearer to reliably access the property you care about: result.name

然后,可靠地访问您关心的属性更清楚:result.name