通过Javascript语法从JSON对象读取[重复](Reading from a JSON object via Javascript syntax [duplicate])
这个问题在这里已经有了答案:
- 访问/处理(嵌套)对象,数组或JSON 18答案
我想从JSON对象读取一些值并使用它们来填充一个html模板。 我似乎可以找出从JSON文件中读取的正确语法。 例如,当我想读第一个对象的“角色”时,我写道:
console.log("first object: "+ ((obj[0].roles)[0]).name)
以上工作并返回“第一个对象:评论的东西”。
然后,当我尝试在第一个对象中读取“srcLanguageSentence”下的动词文本时,我写道:
console.log("first verb: "+ ((obj[0].srcLanguageSentence.verb[2]).text)
我希望看到“第一个动词:评论”,但这不会发生。 我的语法错在哪里?请参阅下面的JSON对象文件的一个片段:
[ { "description": "", "roles": [ { "name": "thing commented on" }, { "name": "commentor" } ], "srcLanguageSentence": { "roles": [ { "beginOffset": 23, "endOffset": 30, "name": "thing commented on", "text": "on them" }, { "beginOffset": 5, "endOffset": 7, "name": "commentor", "text": "We" } ], "text": " `` We wo n't comment on them . '' ", "verb": { "beginOffset": 15, "endOffset": 22, "text": "comment" } }, "tgtLanguageSentences": [ { "roles": [ { "beginOffset": 1, "endOffset": 31, "name": "thing commented on", "text": "Weitere Aspekte der Kommission" }, { "beginOffset": 44, "endOffset": 47, "name": "commentor", "text": "ich" }, { "beginOffset": 48, "endOffset": 55, "name": "negation", "text": "nicht ." } ], "text": " Weitere Aspekte der Kommission kommentiere ich nicht . ", "verb": { "beginOffset": -1, "endOffset": -1, "sense": "COMMENT, intransitive", "text": "kommentieren" } } ], "verb": "KOMMENTIEREN" }, ]
This question already has an answer here:
I am trying to read some values from a JSON object and use them to populate an html template. I can seem to figure out the right syntax to read from the JSON file. For example, when I want to read in the first object's "role", I write:
console.log("first object: "+ ((obj[0].roles)[0]).name)
The above works and returns "first object: thing commented on".
Then when I try to read the text of the verb under the "srcLanguageSentence" within the first object, I write:
console.log("first verb: "+ ((obj[0].srcLanguageSentence.verb[2]).text)
I expect to see "first verb: comment" but that does not happen. Where is my syntax wrong here?Please see a snippet of the JSON object file below:
[ { "description": "", "roles": [ { "name": "thing commented on" }, { "name": "commentor" } ], "srcLanguageSentence": { "roles": [ { "beginOffset": 23, "endOffset": 30, "name": "thing commented on", "text": "on them" }, { "beginOffset": 5, "endOffset": 7, "name": "commentor", "text": "We" } ], "text": " `` We wo n't comment on them . '' ", "verb": { "beginOffset": 15, "endOffset": 22, "text": "comment" } }, "tgtLanguageSentences": [ { "roles": [ { "beginOffset": 1, "endOffset": 31, "name": "thing commented on", "text": "Weitere Aspekte der Kommission" }, { "beginOffset": 44, "endOffset": 47, "name": "commentor", "text": "ich" }, { "beginOffset": 48, "endOffset": 55, "name": "negation", "text": "nicht ." } ], "text": " Weitere Aspekte der Kommission kommentiere ich nicht . ", "verb": { "beginOffset": -1, "endOffset": -1, "sense": "COMMENT, intransitive", "text": "kommentieren" } } ], "verb": "KOMMENTIEREN" }, ]
原文:https://stackoverflow.com/questions/31014063