如何用缩进的JSON初始化JSON成员?

如何用缩进的JSON初始化JSON成员?,json,angular,typescript,Json,Angular,Typescript,这里,变量some用JSON字符串初始化 为了便于阅读,我需要扩展,因此缩进 export class MyComponent implements OnInit { some:any = JSON.parse('[{"id":"EN","fill":"blue","classb":"FR someclass"},{"id":"US","fill":"hsl(240, 100%, 35%)","classb":"someclass"},{"id":"ES","fill":"hsl(2

这里,变量
some
用JSON字符串初始化 为了便于阅读,我需要扩展,因此缩进

export class MyComponent implements OnInit {
      some:any = JSON.parse('[{"id":"EN","fill":"blue","classb":"FR someclass"},{"id":"US","fill":"hsl(240, 100%, 35%)","classb":"someclass"},{"id":"ES","fill":"hsl(240, 100%, 60%)","classb":"someclass"},{"id":"IT","fill":"hsl(240, 100%, 90%)","classb":"someclass"}]');

      getStyle(zoneId:string):String{
        var test = this.some.find(x => x.id === zoneId);
        if( test === undefined) return "#000000";
        if( test.fill != undefined) return test.fill;
           return "red";
      }
}
如何使用缩进的JSON来初始化TypeScript/Angular类成员?

您可以创建一个带有倒勾的字符串,它允许为格式化的JSON字符串换行:

class MyComponent {
  some = JSON.parse(`
    [
      {
        "id": "EN",
        "fill": "blue",
        "classb": "FR someclass"
      },
      {
        "id": "US",
        "fill": "hsl(240, 100%, 35%)",
        "classb": "someclass"
      },
      {
        "id": "ES",
        "fill": "hsl(240, 100%, 60%)",
        "classb": "someclass"
      },
      {
        "id": "IT",
        "fill": "hsl(240, 100%, 90%)",
        "classb": "someclass"
      }
    ]
    `);
}
您可以创建一个带有倒勾的字符串,它允许为格式化的JSON字符串换行:

class MyComponent {
  some = JSON.parse(`
    [
      {
        "id": "EN",
        "fill": "blue",
        "classb": "FR someclass"
      },
      {
        "id": "US",
        "fill": "hsl(240, 100%, 35%)",
        "classb": "someclass"
      },
      {
        "id": "ES",
        "fill": "hsl(240, 100%, 60%)",
        "classb": "someclass"
      },
      {
        "id": "IT",
        "fill": "hsl(240, 100%, 90%)",
        "classb": "someclass"
      }
    ]
    `);
}

只需使用一个对象。JSON实际上是JavaScript的一部分(出于所有目的)

您根本不需要嵌入字符串

例如,您可以编写以下代码

export class MyComponent implements OnInit {
  some = [ // note removed `: any` as it degrades tooling esp when there is an initializer
    {"id": "EN", "fill": "blue", "classb": "FR someclass"},
    {"id": "US", "fill": "hsl(240, 100%, 35%)", "classb": "someclass"},
    {"id": "ES", "fill": "hsl(240, 100%, 60%)", "classb": "someclass"},
    {"id": "IT", "fill": "hsl(240, 100%, 90%)", "classb": "someclass"}
  ];

  getStyle(zoneId: string): string { // note correct type is `string` not `String`
    var test = this.some.find(x => x.id === zoneId);
    if (test === undefined) return "#000000";
    if (test.fill != undefined) return test.fill;
    return "red";
  }
}

这提供了完整的工具和语言级别的支持,同时允许使用JavaScript允许的所有可能的表达格式。

只需使用一个对象。JSON实际上是JavaScript的一部分(出于所有目的)

您根本不需要嵌入字符串

例如,您可以编写以下代码

export class MyComponent implements OnInit {
  some = [ // note removed `: any` as it degrades tooling esp when there is an initializer
    {"id": "EN", "fill": "blue", "classb": "FR someclass"},
    {"id": "US", "fill": "hsl(240, 100%, 35%)", "classb": "someclass"},
    {"id": "ES", "fill": "hsl(240, 100%, 60%)", "classb": "someclass"},
    {"id": "IT", "fill": "hsl(240, 100%, 90%)", "classb": "someclass"}
  ];

  getStyle(zoneId: string): string { // note correct type is `string` not `String`
    var test = this.some.find(x => x.id === zoneId);
    if (test === undefined) return "#000000";
    if (test.fill != undefined) return test.fill;
    return "red";
  }
}

这提供了完整的工具和语言级别的支持,同时允许使用JavaScript允许的所有可能的表达格式。

除了编写之外?将其设置为类外或配置文件中的常量为什么要这样做?@Aluan Haddad为了可读性,如问题中所述…@user1767316 JSON是JavaScript为什么不使用对象?例如,
some={“id”:“EN”,“fill”:“blue”}。为什么使用字符串?如果你按照我的建议去做,你不仅能够灵活地设置格式,还能自动获得语言支持、验证和智能感知,而不仅仅是编写?将其设置为类外或配置文件中的常量为什么要这样做?@Aluan Haddad为了可读性,如问题中所述…@user1767316 JSON是JavaScript为什么不使用对象?例如,
some={“id”:“EN”,“fill”:“blue”}。为什么使用字符串?如果你按照我的建议去做,你不仅能够灵活地设置格式,而且还将自动获得语言支持、验证和智能感知。你为什么要写它而不是
some=[{id:“EN”,…];
?这是OP的问题。你为什么要写它而不是
some=[{id:“EN”,…]
?这是一个关于OP的问题。谢谢@Aluan Haddad。除了你的评论,你还回答了我可以同时测试的问题。我把正确答案检查留给tony19,他找到了我最初有效问题的解决方案。谢谢@Aluan Haddad。除了你的评论,你还回答了我可以同时测试的问题。我离开righ我没有回答tony19的问题,他找到了我最初有效问题的解决方案。