[英]Can't access value from JSON
I have this JSON string: 我有这个JSON字符串:
{
"attachedFiles": [{
"link": "/site.com/dir?id=12993&SESSION=40af90dd-c1f3-4678-93e5-a4b36f3597b0&SESSIONTICKET=SESS:67bf209be2",
"fileName": "file1.txt",
"docDate": "24.02.2014",
"docTime": "13:54",
"docId": "12993"
}],
"requestId": 48,
"tasksId": 0,
"workId": 10558
}
I'm converting it like this: 我正在这样转换:
var resdata = xhr.responseText; // the string response from the server
var resObj = JSON.parse(resdata);
And then I'm trying to access(print the value) fileName
inside of the attachedFiles
object by the code below: 然后我尝试通过以下代码访问(打印值) attachedFiles
对象内部的fileName
:
console.log(resObj.attachedFiles.fileName);
It always returns undefined
. 它总是返回undefined
。 I know that I'm mising something real small here, but I'm not able to spot it. 我知道我在这里误会一些很小的东西,但我无法发现它。
attachedFiles
is array. attachedFiles
是数组。 So try access the array content using indexer 因此,尝试使用索引器访问数组内容
resObj.attachedFiles[0].fileName // 0th index, 1st Element
To access all elements in the array. 访问数组中的所有元素。 Thanks to @Cerbus comment 感谢@Cerbus评论
for(var i = 0, l = resObj.attachedFiles.length; i < l;i++)
{
console.log(resObj.attachedFiles[i].fileName);
}
attachedFiles
是一个array
因此使用indexer
是从零开始的索引,因此第一个元素将位于零索引。
console.log(resObj.attachedFiles[0].fileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.