从http.get json文件

时间:2018-07-18 11:00:27

标签: json angular

我有一个json文件,其中几乎没有数据(data.json)

{
  "currency": {
    "SAR": "Saudi Riyal",
    "EUR": "Euro",
    "KWD": "Kuwaiti Dinar",
    "OMR": "Omani Rial",
    "INR": "Indian Rupee",
    "AED": "UAE Dirham",
    "EGP": "Egyptian Pound",
    "PKR": "Pakistan Rupee",
    "USD": "US Dollar",
    "LKR": "Sri Lanka Rupee",
    "BHD": "Bahraini Dinar",
    "CZK": "Czech Koruna",
    "JOD": "Jordanian Dinar",
    "KZT": "Tenge",
    "NPR": "Nepalese Rupee",
    "QAR": "Qatari Rial",
    "RUB": "Russian Ruble",
    "THB": "Baht"
  },
  "statusCode": [ "FAILED", "VOIDED", "IN_PROGRESS", "SUCCESS", "PARTIAL_REFUND", "CAPTURED", "CAPTURE_FAILED", "REFUNDED" ],
  "paymentMethods": [ "BSP", "CARD", "VCHR", "INVC" ]
}

ve got the data with http get, but the problem is if I表示我正在得到

""{\r\n  \"currency\": {\r\n    \"SAR\": \"Saudi Riyal\",\r\n    \"EUR\": \"Euro\",\r\n    \"KWD\": \"Kuwaiti Dinar\",\r\n    \"OMR\": \"Omani Rial\",\r\n    \"INR\": \"Indian Rupee\",\r\n    \"AED\": \"UAE Dirham\",\r\n    \"EGP\": \"Egyptian Pound\",\r\n    \"PKR\": \"Pakistan Rupee\",\r\n    \"USD\": \"US Dollar\",\r\n    \"LKR\": \"Sri Lanka Rupee\",\r\n    \"BHD\": \"Bahraini Dinar\",\r\n    \"CZK\": \"Czech Koruna\",\r\n    \"JOD\": \"Jordanian Dinar\",\r\n    \"KZT\": \"Tenge\",\r\n    \"NPR\": \"Nepalese Rupee\",\r\n    \"QAR\": \"Qatari Rial\",\r\n    \"RUB\": \"Russian Ruble\",\r\n    \"THB\": \"Baht\"\r\n  },\r\n  \"statusCode\": [ \"FAILED\", \"VOIDED\", \"IN_PROGRESS\", \"SUCCESS\", \"PARTIAL_REFUND\", \"CAPTURED\", \"CAPTURE_FAILED\", \"REFUNDED\" ],\r\n  \"paymentMethods\": [ \"BSP\", \"CARD\", \"VCHR\", \"INVC\" ]\r\n}\r\n\r\n""

ve got the response in variable success, but I can t遍历。 成功的样子,

"{
  "currency": {
    "SAR": "Saudi Riyal",
    "EUR": "Euro",
    "KWD": "Kuwaiti Dinar",
    "OMR": "Omani Rial",
    "INR": "Indian Rupee",
    "AED": "UAE Dirham",
    "EGP": "Egyptian Pound",
    "PKR": "Pakistan Rupee",
    "USD": "US Dollar",
    "LKR": "Sri Lanka Rupee",
    "BHD": "Bahraini Dinar",
    "CZK": "Czech Koruna",
    "JOD": "Jordanian Dinar",
    "KZT": "Tenge",
    "NPR": "Nepalese Rupee",
    "QAR": "Qatari Rial",
    "RUB": "Russian Ruble",
    "THB": "Baht"
  },
  "statusCode": [ "FAILED", "VOIDED", "IN_PROGRESS", "SUCCESS", "PARTIAL_REFUND", "CAPTURED", "CAPTURE_FAILED", "REFUNDED" ],
  "paymentMethods": [ "BSP", "CARD", "VCHR", "INVC" ]
}

"

我还将显示我的http获取

 getFileContent = (fileName) => {
        let apiUrl = './assets/data/' + fileName;
        return this.http.get(apiUrl, { responseType: 'text' });
    }

this.apiService.getFileContent("data.json").subscribe(
        success => {
            console.log(success);
        }, error => {
            console.log(error);
        }
    )

请帮助我找到s wrong, I的最新知识。 我的角度版本是6。 这些数据是为下拉映射创建的,请告诉我是否有更好的方法

1 个答案:

答案 0 :(得分:4)

发现我犯了什么错误,应该给出响应类型:json

 getFileContent = (fileName) => {
        let apiUrl = './assets/data/' + fileName;
        return this.http.get(apiUrl, { responseType: 'json' });
    }