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

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

问题描述:

这是我的 json:

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

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

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

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

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?

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

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

您可以使用 for..in 构造来迭代对象的任意属性:

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

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