Initializing JSON object in TypeScript

前端 未结 3 788
情深已故
情深已故 2021-02-02 12:28

I\'m new to TypeScript and I\'m stuck at working with JSON. I need to create a simple JSON object and I keep on failing doing so. Here are my first attempts:

out         


        
相关标签:
3条回答
  • 2021-02-02 13:04

    It worked for me in Angular 2.4.0 Stable by doing this:

    var request: any = {};
    request.allocation = allocationFigure;
    
    0 讨论(0)
  • 2021-02-02 13:15

    You can also do the following

    const rememberUser:JSON = <JSON><unknown>{
            "username": this.credentialsForm.value.username,
            "password": this.credentialsForm.value.password
          }
    
    0 讨论(0)
  • 2021-02-02 13:27

    I've eventually figured it out. All I had to do was to create data to "any" variable like this:

    output: JSON;
    obj: any = 
    {
    "col1":{"Attribute1": "value1", "Attribute2": "value2", "Attribute3": "value3"},
    "col2":{"Attribute1": "value4", "Attribute2": "value5", "Attribute3": "value6"}, 
    "col3":{"Attribute1": "value7", "Attribute2": "value8", "Attribute3": "value9"} 
    };
    

    and then cast it to JSON object:

    this.output = <JSON>this.obj;
    
    0 讨论(0)
提交回复
热议问题