在JavaScript中从对象获取值

在JavaScript中从对象获取值,javascript,Javascript,我需要帮助在ajax调用中从响应对象获取值 代码片段 $.each(responseJson.slice(0,7), function (index) { var resp_JSON=responseJson[index]; console.log(resp_JSON); 在控制台中,resp_JSON是对象{17130003:来自缓存的消息字符串} 现在,响应Json没有名称标记,因此我可以执行resp_Json.id或其他操作&获取值。它只是有价值 我试过了 resp_JSON[0]//错

我需要帮助在ajax调用中从响应对象获取值

代码片段

$.each(responseJson.slice(0,7), function (index) {
var resp_JSON=responseJson[index];
console.log(resp_JSON);
在控制台中,resp_JSON是对象{17130003:来自缓存的消息字符串}

现在,响应Json没有名称标记,因此我可以执行resp_Json.id或其他操作&获取值。它只是有价值

我试过了

resp_JSON[0]//错误

resp_JSON.Object[0]//错误

我需要在两个单独的javascript变量中从缓存中获取17130003&一个消息字符串。

以便获取对象的键和值。您可以这样做:

var keys = Object.keys(resp_JSON); // [17130003]
var values = Object.values(resp_JSON); // ["A message string from the cache"]
请注意,这两个都是数组,您可以简单地在数组中循环以处理每个值/键


此外,正如@Hassan所指出的,如果您想要实现,您可以通过resp_JSON['some_key']获得特定值。

实际上您需要访问resp_JSON['17130003'],如果键是动态的,您可以使用Object.valuesresp_JSON和Object.keysresp_JSON@HassanImam谢谢你,伙计,成功了。