使用jQuery从对象获取数据值

时间:2016-03-11 13:47:32

标签: jquery

我使用jQuery函数将数据传递给服务器。

data : {"id" : "3"}

如何从对象中获取3值?假设我可以访问data并输出{"id" : "3"}我尝试了2种方法,但都没有效果。

方法1:

我尝试使用以下方法获取值:

$(this)[0].data.id

结果: undefined

方法2:

JSON.parse($(this)[0].data).id

结果: Unexpected token

打印到控制台如下:

[Object]
0
:
Object
accepts
:
Object
async
:
true
contentType
:
"application/json; charset=utf-8"
contents
:
Object
converters
:
Object
crossDomain
:
false
data
:
"{ 'id': '3' }"
dataType
:
"json"
dataTypes
:
Array[2]
error
:
(uploadifyDiv, response, a, b)
flatOptions
:
Object
global
:
true
hasContent
:
true
isLocal
:
false
jsonp
:
"callback"
jsonpCallback
:
()
processData
:
true
responseFields
:
Object
success
:
(response)
type
:
"POST"
url
:
"/TestApp/Pages/Uploadify.aspx/FileUploaded"
xhr
:
ci()
__proto__
:
Object
length
:
1
__proto__
:
Object[0]

1 个答案:

答案 0 :(得分:1)

您可以data.id访问它。

console.log(data.id);

如果是字符串化,则需要先解析它:

data = JSON.parse(data);
console.log(data.id);

或者@Alon评论过,它可能是另一个对象中的对象。在这种情况下,请执行

 console.log(parentObj.data.id);