在JSON文档中使用JavaScript读取多行字符串

179 浏览
0 Comments

在JSON文档中使用JavaScript读取多行字符串

这个问题已经在这里得到答案了:

JSON中允许使用多行字符串吗?

我有一个JSON文档,其中一些数据是多行字符串

我尝试了以下选项,但似乎都不起作用

例如

[
  {
    "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");
        }
    }

admin 更改状态以发布 2023年5月25日
0
0 Comments

我按照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"}]

0