gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:53:16 28 4
gpt4 key购买 nike

这是我的 json:

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

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

我的 json 结构是这样的原因是我通过 jquery 调用的 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 构造来遍历对象的任意属性:

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

关于javascript - 在不知道键名的情况下访问 JSON 对象的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5113847/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com