Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

json - Angular convert ids of http request from long to string

In my Backend I have IDs as long. Sadly Angular or better Javascript does not like long ids very much and rounds large numbers. Since that is not optimal for IDs I need to convert it from long to string before parsing it into a json object:

this.http.get<Player>(this.getUrl() + '/' + id);

My Player class:

export class Player {
   id: string;
   name: string;
}

How can I covert the ids to string before making a object from them?

This is the backend answer:

{ "id": 12345, "name": "Test" }

This is what I need to convert it to before converting to an object:

{ "id": "12345", "name": "Test" }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use pipe and map to alter the response;

this.http.get(this.getUrl() + '/' + id).pipe(map((player) => {
  player.id = ""+player.id;
  return player;
}));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...