【发布时间】:2016-02-20 19:27:22
【问题描述】:
我正在尝试格式化我的 JSON 文件,以便在解析时显示为带有如下换行符的段落:
This is one line.
This is a second line.
This is a third line.
我试过 \n \\n \p\n 但无济于事。
这是我想要解析文本的 iPhone 屏幕:
这是 JSON 文件。特别是 "body" 标签是我想要这样做的地方:
{
"id": 1,
"jsonrpc": "2.0",
"total": 5,
"result": [
{
"id": 1,
"guid": "1d4aa3b2-c059-4fa7-a751-9bca735e4ebb",
"thumb": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1-thumb.jpg",
"picture": "http://skounis.s3.amazonaws.com/mobile-apps/barebone-glossy/photo-1.jpg",
"title": "Continuing Education 2015 Class Schedule",
"body": "October 24, 2015 from 9am - 1pm\nOctober 24, 2015 from 9am - 1pm",
"tags": ["continuing ed"]
}
]
}
下载 JSON 的 new.service.js 文件:
(function() {
'use strict';
angular
.module('barebone.news')
.factory('newsService', newsService);
newsService.$inject = ['$http', '$q'];
/* @ngInject */
function newsService($http, $q) {
var url = 'https://s3-us-west- 2.amazonaws.com/cisnerostraininggroup/news.json';
var result = [];
var service = {
all: all,
get: get
};
return service;
// *******************************************************
// http://stackoverflow.com/questions/17533888/s3-access-control-allow- origin-header
function all(callback){
$http.get(url)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
result = data.result;
callback(result);
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('ERROR (News):' + status);
callback(result);
});
}
function get(articleId) {
// we take an article from cache but we can request ir from the server
for (var i = 0; i < result.length; i++) {
if (result[i].id === articleId) {
return $q.when(result[i]);
}
}
return $q.when(null);
}
}
})();
【问题讨论】:
-
你在说什么代码?我发布了 JSON 文件。
-
你能给我看一个代码示例吗?我不知道你用换行符替换
\n是什么意思。
标签: ios json cordova formatting ionic