3

First things first, this is my first time using Angular 7; I started making an application using Angular 7 with a c# back end and have need for serializing an object in my component/service before sending it to my controller/service.

Something like:

export class jsonTest  {
    json: string;
    obj: myType = {} as myType;

    this.obj.someProperty = 1234;
    this.obj.anotherProperty = 'test';

    someMethod() {
        this.json = //convert obj to json
        anotherMethod(this.json);
    }
}

In my search to figure out how to accomplish this, I've come across two popular suggestions, one being JSON.stringify() and another being toJson().

However, JSON.stringify() throws a compile error that symbol JSON cannot be resolved, probably it is located in an inaccessible module.

Trying toJson(), it isn't recognized as a hook of any sort.

Is there some import that I'm missing? Looking through angular documentation doesn't shed any light on my issue.

At this point, I'm considering just serializing the JSON manually, but I'd really like to avoid doing that if I can. Any suggestions?

expenguin
  • 1,052
  • 2
  • 21
  • 42

1 Answers1

-1

You have some errors in your typescript. Try doing this instead.

export class JsonTest implements OnInit {
json: string;
obj: MyType = new MyType();

ngOnInit(): void {
    this.obj.someProperty = 1234;
    this.obj.anotherProperty = 'test';
}
someMethod() {
    this.json = JSON.stringify(this.obj);
    anotherMethod(this.json);
}}
Paul Hebert
  • 296
  • 3
  • 8
  • The issue was actually Resharper throwing errors in VS2016. That was just some psudo code anyway for examples purpose :) Thanks anyways! – expenguin Dec 13 '18 at 15:07