Having a JSON document with multiline string data is causing issues for me.
I have attempted multiple options, but none of them have successfully solved the problem.
For example:
[
{
"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."
}
]
or
[
{
"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.`
}
]
or
[
{
"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.'
}
]
or using \n as suggested in the comments, which also did not work.
[
{
"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."
}
]
Despite trying various other combinations, any newline characters seem to disrupt the code functionality.
Below is the Angular 2/Javascript code that reads the JSON file:
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");
}
}