如何解析离子3中的json字符串。我的要求很简单,即只需解析存储在常量中的json,并将其显示在离子中具有上一个和下一个按钮的滑块中。只需帮助我解析数据和存储它在阵列中。我以前没有离子和角度的经验。
我尝试过的代码:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http,Headers } from '@angular/http';
import 'rxjs/add/operator/map';
//////this is the json format.
const questions={
"setId": 1,
"setName": "Set-A",
"totalQuestions": 3,
"markPerQuestion": 1,
"totalDuration": 10,
"questions": [
{
"questionId": 1,
"questionName": "What is the term used for describing the judgmental part of problem solving?",
"questionInstruction": "Select Correct Answer",
"questionAnswer": "a",
"img": "",
"questionOptions": [
{
"optionId": "a",
"optionValue": "Heuristic"
},
{
"optionId": "b",
"optionValue": "Critical"
},
{
"optionId": "c",
"optionValue": "Value based"
},
{
"optionId": "d",
"optionValue": "Analytical"
},
{
"optionId": "e",
"optionValue": "None of the above"
}
]
},
{
"questionId": 2,
"questionName": "Decision support programs are designed to help managers make:",
"questionInstruction": "Select Correct Answer",
"questionAnswer": "d",
"img": "",
"questionOptions": [
{
"optionId": "a",
"optionValue": "vacation schedules"
},
{
"optionId": "b",
"optionValue": "budget projections"
},
{
"optionId": "c",
"optionValue": "All of the above"
},
{
"optionId": "d",
"optionValue": "business decisions"
},
{
"optionId": "e",
"optionValue": "None of the above"
}
]
},
{
"questionId": 3,
"questionName": "What was originally called the imitation game by its creator?",
"questionInstruction": "Select Correct Answer",
"questionAnswer": "b",
"img": "",
"questionOptions": [
{
"optionId": "a",
"optionValue": "Cybernetics"
},
{
"optionId": "b",
"optionValue": "The Turing Test"
},
{
"optionId": "c",
"optionValue": "LISP"
},
{
"optionId": "d",
"optionValue": "The Logic Theorist"
},
{
"optionId": "e",
"optionValue": "None of the above"
}
]
}
]
};
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController) {
}
ionViewDidLoad(){
this.parsejsondata();
}
parsejsondata(){
const userStr = JSON.stringify(questions);
JSON.parse(userStr, (key, value) => {
if (typeof value === 'string') {
return value.toUpperCase();
}
console.log(JSON.parse(value));
return value;
});
}
}
它引发了一个错误:
main.js:1无法加载资源:服务器响应状态 500(内部服务器错误)(索引):1拒绝执行脚本 来自“http://localhost:8100/build/main.js”,因为它的MIME类型 ('text / html')不可执行,严格的MIME类型检查是 启用。