我有一个JSON对象,它返回以下值。
{
"articles":[
{
"paper-id":"id",
"paper-id-type":"type",
"title":"title",
"url":"url",
"abstract":"abs",
"date":"date",
"publication-forum":"forum",
"publication-forum-type":"type",
"authors":"auth",
"keywords":"key1,key2"
}
}
我尝试通过JavaScript访问这些结果。 首先,我创建了一个数组并将这些结果分配给数组。
数组(命名文章)对象的内容如下所示;
abstract: "xxx"
authors: "yyy"
date: "1111"
keywords: "key1, key2"
paper-id: "abc"
paper-id-type: "xxx"
publication-forum: "yyy"
publication-forum-type: "zzz"
title: "www"
url: "url"
然后我尝试使用格式
访问这些元素中的每个值articles[0]["abstract"]
适用于没有“ - ”字符的元素。所以当我试图提取paper-id;
articles[0]["paper-id"]
我收到了error [Exception: SyntaxError: Unexpected token []
有谁知道如何解决这个问题?
答案 0 :(得分:3)
问题是因为您忘记关闭JSON中的[]
和{}
你JSON应该是这样的
{
"articles":[
{
"paper-id":"id",
"paper-id-type":"type",
"title":"title",
"url":"url",
"abstract":"abs",
"date":"date",
"publication-forum":"forum",
"publication-forum-type":"type",
"authors":"auth",
"keywords":"key1,key2"
}]
}
答案 1 :(得分:1)
abc = {
"articles": [{
"paper-id": "id",
"paper-id-type": "type",
"title": "title",
"url": "url",
"abstract": "abs",
"date": "date",
"publication-forum": "forum",
"publication-forum-type": "type",
"authors": "auth",
"keywords": "key1,key2"
}
]
};
for (i in abc['articles'][0]) {
console.log(abc['articles'][0][i]);
}