我有一个JSON文档,其中一些数据是多行字符串
我尝试了以下选项,但似乎没有任何工作
e.g。
[
{
"someString" : "A rather long string of English text, an error message \
actually that just keeps going and going -- an error \
message to make the Energizer bunny blush (right through \
those Schwarzenegger shades)! Where was I? Oh yes, \
you've got an error and all the extraneous whitespace is \
just gravy. Have a nice day."
}
]
或
[
{
"someString" : `A rather long string of English text, an error message` +
`actually that just keeps going and going -- an error` +
`message to make the Energizer bunny blush (right through` +
`those Schwarzenegger shades)! Where was I? Oh yes,` +
`you've got an error and all the extraneous whitespace is` +
`just gravy. Have a nice day.`
}
]
或
[
{
"someString" : 'A rather long string of English text, an error message' +
'actually that just keeps going and going -- an error' +
'message to make the Energizer bunny blush (right through' +
'those Schwarzenegger shades)! Where was I? Oh yes,' +
'you've got an error and all the extraneous whitespace is' +
'just gravy. Have a nice day.'
}
]
或与评论中建议的\ n组合,但这也不起作用。
[
{
"shortStory": "A rather long string of English text, an error message\n
actually that just keeps going and going -- an error \n
message to make the Energizer bunny blush (right through\n
those Schwarzenegger shades)! Where was I? Oh yes,\n
you've got an error and all the extraneous whitespace is\n
just gravy. Have a nice day."
}
]
以及其他各种组合。只要我有新行,代码就不起作用。
以下是读取JSON文件的代码(Angular 2 / Javascript)
import {
Injectable
} from '@angular/core';
import {
Http,
Headers,
RequestOptions,
Response
} from '@angular/http';
import {
Observable,
Subject
} from 'rxjs/Rx';
import 'rxjs/Rx'; //get everything from Rx
import 'rxjs/add/operator/toPromise';
import {
IArticle
} from './article';
@Injectable()
export class ArticleService {
private jsonFileURL: string = "./assets/data/article-data.json";
constructor(private http: Http) {}
//
getArticles(): Observable < IArticle[] > {
return this.http.get(this.jsonFileURL).map((response: Response) => {
return <IArticle[] > response.json()
}).catch(this.handleError);
}
//
private handleError(errorResponse: Response) {
//console.log(errorResponse.statusText);
return Observable.throw(errorResponse.json().error || "Server error");
}
}
答案 0 :(得分:0)
我按照@Patrick的建议用\ n替换换行文字,希望有更好的方法来使用换行符读取JSON数据。
[{"shortStory": "The free bird leaps\n on the back of the wind\nand floats downstream\ntill the current ends\nand dips his wings\nin the orange sun rays\nand dares to claim the sky.\n\nBut a bird that stalks\ndown his narrow cage\ncan seldom see through\nhis bars of rage\nhis wings are clipped and\nhis feet are tied\nso he opens his throat to sing.\n\nThe caged bird sings\nwith fearful trill\nof the things unknown\nbut longed for still\nand his tune is heard\non the distant hill\nfor the caged bird\nsings of freedom\n\n The free bird thinks of another breeze\n and the trade winds soft through the sighing trees\n and the fat worms waiting on a dawn-bright lawn\n\n and he names the sky his own.\n\n But a caged bird stands on the grave of dreams\n his shadow shouts on a nightmare scream\n his wings are clipped and his feet are tied\nso he opens his throat to sing\n\nThe caged bird sings\n with a fearful trill\n of things unknown\n but longed for still\n and his tune is heard\n on the distant hill\n for the caged bird \n sings of freedom"}]