angularjs - Multiline String reading in javascript from JSON document -
this question has answer here:
- multiline strings in json 13 answers
i have json document of data multi line string
i have tried following options , none of them seem work
e.g.
[ { "somestring" : "a rather long string of english text, error message \ keeps going , going -- error \ message make energizer bunny blush (right through \ schwarzenegger shades)! i? oh yes, \ you've got error , extraneous whitespace \ gravy. have nice day." } ] or
[ { "somestring" : `a rather long string of english text, error message` + `actually keeps going , going -- error` + `message make energizer bunny blush (right through` + `those schwarzenegger shades)! i? oh yes,` + `you've got error , extraneous whitespace is` + `just gravy. have nice day.` } ] or
[ { "somestring" : 'a rather long string of english text, error message' + 'actually keeps going , going -- error' + 'message make energizer bunny blush (right through' + 'those schwarzenegger shades)! i? oh yes,' + 'you've got error , extraneous whitespace is' + 'just gravy. have nice day.' } ] or combination \n suggested in comments, didn't work either.
[ { "shortstory": "a rather long string of english text, error message\n keeps going , going -- error \n message make energizer bunny blush (right through\n schwarzenegger shades)! i? oh yes,\n you've got error , extraneous whitespace is\n gravy. have nice day." } ] and various other combinations. long have new line code doesn't work.
here code (angular 2/javascript) reads json file
import { injectable } '@angular/core'; import { http, headers, requestoptions, response } '@angular/http'; import { observable, subject } 'rxjs/rx'; import 'rxjs/rx'; //get rx import 'rxjs/add/operator/topromise'; import { iarticle } './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"); } }
i replaced newline literals \n suggested @patrick, there better way read json data newlines.
[{"shortstory": "the free bird leaps\n on of wind\nand floats downstream\ntill current ends\nand dips wings\nin orange sun rays\nand dares claim sky.\n\nbut bird stalks\ndown narrow cage\ncan seldom see through\nhis bars of rage\nhis wings clipped and\nhis feet tied\nso opens throat sing.\n\nthe caged bird sings\nwith fearful trill\nof things unknown\nbut longed still\nand tune heard\non distant hill\nfor caged bird\nsings of freedom\n\n free bird thinks of breeze\n , trade winds soft through sighing trees\n , fat worms waiting on dawn-bright lawn\n\n , names sky own.\n\n caged bird stands on grave of dreams\n shadow shouts on nightmare scream\n wings clipped , feet tied\nso opens throat sing\n\nthe caged bird sings\n fearful trill\n of things unknown\n longed still\n , tune heard\n on distant hill\n caged bird \n sings of freedom"}]
Comments
Post a Comment