我需要帮助从 ajax 调用中的响应对象获取值
代码片段
$.each(responseJson.slice(0,7), function (index) {
var resp_JSON=responseJson[index];
console.log(resp_JSON);
在控制台中resp_JSON
是Object {17130003: "A message string from the cache"}
现在,响应 Json 没有名称标签,因此我可以resp_JSON.id
做某事并获取值。它只有价值观。
我试过了
resp_JSON[0];
//错误
resp_JSON.Object[0];
//错误
我需要在两个单独的 javascript 变量中获取17130003
& A message string from the cache
。
为了获取对象的键和值。你可以这样做:
var keys = Object.keys(resp_JSON); // [17130003]
var values = Object.values(resp_JSON); // ["A message string from the cache"]
请注意,两者都是数组,您可以简单地遍历数组来处理每个值/键。
此外,正如@Hassan 所指出的,resp_JSON['some_key']
如果这是您想要实现的目标,您可以获得特定的价值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句