Javascript 在不知道键名的情况下访问 JSON 对象的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5113847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:43:04  来源:igfitidea点击:

Accessing elements of JSON object without knowing the key names

javascriptasp.netjsonvb.netfor-loop

提问by Radu

Here's my json:

这是我的json:

{"d":{"key1":"value1",
      "key2":"value2"}}

Is there any way of accessing the keys and values (in javascript) in this array without knowing what the keys are?

有没有办法在不知道键是什么的情况下访问这个数组中的键和值(在javascript中)?

The reason my json is structured like this is that the webmethod that I'm calling via jquery is returning a dictionary. If it's impossible to work with the above, what do I need to change about the way I'm returning the data?

我的 json 结构像这样的原因是我通过 jquery 调用的 webmethod 正在返回一个字典。如果无法使用上述方法,我需要更改返回数据的方式吗?

Here's an outline of my webmethod:

这是我的网络方法的概述:

<WebMethod()> _
Public Function Foo(ByVal Input As String) As Dictionary(Of String, String)
    Dim Results As New Dictionary(Of String, String)

    'code that does stuff

    Results.Add(key,value)
    Return Results
End Function

回答by Ates Goral

You can use the for..inconstruct to iterate through arbitrary properties of your object:

您可以使用该for..in构造来遍历对象的任意属性:

for (var key in obj.d) {
    console.log("Key: " + key);
    console.log("Value: " + obj.d[key]);
}

回答by Xenethyl

Is this what you're looking for?

这是你要找的吗?

var data;
for (var key in data) {
   var value = data[key];
   alert(key + ", " + value);
}

回答by KARTHIKEYAN.A

Use for loop to achieve the same.

使用 for 循环来实现相同的效果。

var dlist = { country: [ 'ENGLAND' ], name: [ 'CROSBY' ] }

for(var key in dlist){
     var keyjson = dlist[key];
     console.log(keyjson)
   }

回答by Shivam Chhetri

{
  "d":{
     "key1":"value1",
     "key2":"value2"
    }
}

To access first key write:

要访问第一个密钥写入:

   let firstKey=Object.keys(d)[0];

To access value of first key write:

要访问第一个键写入的值:

let firstValue= d[firstKey];

回答by Bhaumik Mehta

By using word "b", You are still using key name.

通过使用单词“b”,您仍在使用密钥名称。

var info = {
"fname": "Bhaumik",
"lname": "Mehta",
"Age": "34",
"favcolor": {"color1":"Gray", "color2":"Black", "color3":"Blue"}
};

Look at the below snippet.

看看下面的片段。

for(key in info) {
  var infoJSON = info[key];
  console.log(infoJSON);
}

Result would be,

结果是,

Bhaumik
Mehta
Object {color1: "Gray", color2: "Black", color3: "Blue"} 

Don't want that last line to show up? Try following code:

不想显示最后一行?尝试以下代码:

for(key in info) {
  var infoJSON = info[key];
    if(typeof infoJSON !== "object"){
       console.log(infoJSON);
  }
}

This will eliminate Object {color1: “Gray”, color2: “Black”, color3: “Blue”}from showing up in the console.

这将消除Object {color1: “Gray”, color2: “Black”, color3: “Blue”}在控制台中的显示。

Now we need to iterate through the variable infoJSON to get array value. Look at the following whole peace of code.

现在我们需要遍历变量 infoJSON 来获取数组值。看看下面的全和平代码。

for(key in info) {
    var infoJSON = info[key];
    if (typeof infoJSON !== "object"){
       console.log(infoJSON);
    }
 }

for(key1 in infoJSON) {
    if (infoJSON.hasOwnProperty(key1)) {
       if(infoJSON[key1] instanceof Array) {
          for(var i=0;i<infoJSON[key1].length;i++) {
             console.log(infoJSON[key1][i]);
          }
        } else {console.log(infoJSON[key1]);}
    }
 }

And now we got the result as

现在我们得到了结果

Bhaumik
Mehta
Gray
Black
Blue

If we use key name or id then it's very easy to get the values from the JSON object but here we are getting our values without using key name or id.

如果我们使用键名或 id,那么很容易从 JSON 对象中获取值,但这里我们在不使用键名或 id 的情况下获取我们的值。