如何在离子3中解析json字符串 . 我的要求很简单,即只需解析存储在常量中的json,并将其显示在离子中具有上一个和下一个按钮的滑块中 . 只需解析数据并将其存储在array.I先前没有离子和角度的经验 .
代码我尝试过:
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类型检查 .