【发布时间】:2018-02-07 04:23:03
【问题描述】:
我有一个简单的服务,它将一个 json 字符串发送到一个 json 组件中。如何访问组件中 json 的各个元素。响应如下。
GetExamResult:
"{"Question":"This is the Question","Answer1":"This is
Answer1","Answer2":"This is Answer2","Answer3":"This is
Answer3","Answer4":"This is Answer4","Correct":1}"
我试过了 this.answer1 = 数据[0].answer1;
但它给出的变量是未定义的。
这样尝试
getData(){
this.httpClient.get('someservice')
.subscribe(
(data:any[]) => {
this.ques1 = data[0].GetExamResult.Question1;
console.log(this.ques1);
}
)
【问题讨论】:
-
你可以使用 JSON.parse,之后你应该使用这样的东西:obj['GetExamResult']['Answer1']
-
@Nour,这就是我在你之前所说的......你在写之前读过答案吗?
标签: javascript json angular rest service