【问题标题】:Multiline String reading in javascript from JSON document [duplicate]从 JSON 文档中读取 javascript 中的多行字符串 [重复]
【发布时间】:2018-01-27 12:18:09
【问题描述】:

我有一个 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.'
  }
]

或者按照 cmets 中的建议与 \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");
        }
    }

【问题讨论】:

  • 因为那不是有效的 JSON。 JSON 使用转义的换行符 (\n) 换行。
  • 正如帕特里克所说,使用“\n”代替+或\。你应该得到想要的输出。
  • 不是这样(这不会解决您的问题,但是)您可以使用反引号作为字符串引号来捕获字符串文字中的文字换行符(尽管这也会捕获左侧的制表符/空格)
  • 另外,{{ 是 JSON 的无效语法。
  • 抱歉打错了,是 [{... }]

标签: javascript angularjs json


【解决方案1】:

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

【讨论】:

  • 没有。这是 JSON 中换行符的唯一有效编码。我不明白这个问题,JSON 不是为了人类可读性,它是一种序列化的数据格式。
  • 我明白了,我对此很平静。但是来自 json.org 的“JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。人类易于读写。机器易于解析和生成。它基于JavaScript 编程语言的一个子集,标准 ECMA-262 第 3 版 - 1999 年 12 月......这些属性使 JSON 成为理想的数据交换语言。”
  • 嗯,相对于其他编码而言,当然。它不是二进制编码,并且属性名称通常是人类可读的,因为它们没有被缩小等。但这并不意味着关于 JSON 的所有内容都是针对人类可读性的。它的主要优先级是在不执行任意 JavaScript 的情况下传输序列化数据的安全格式,无论您是否意识到,这都是您的对象表示法之前尝试做的事情。
猜你喜欢
  • 2019-11-04
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 2013-07-16
  • 2018-02-05
  • 1970-01-01
相关资源
最近更新 更多