1

I have following Json

[{"id":1,"role":"Admin"},{"id":2,"role":"Manager"},{"id":3,"role":"User"}]

I want to iterate this and add to my custom class array which has same properties (id,role,ischecked) in angular typescript.

My Custom class

class Roles{
    ID:number;
    Role:string;
    Checked:Boolean;
} 

How can i achieve it?

5
  • What do you mean by custom class array? Can you share the expected output Commented Sep 29, 2019 at 10:35
  • class Roles{ ID:number; Role:string; Checked:Boolean; } Commented Sep 29, 2019 at 10:36
  • add to array ? stackoverflow.com/questions/351409/… Commented Sep 29, 2019 at 10:37
  • Also, as per your edit since the current array object doesn't have the Checked field - you either have to remove it OR make it optional like Checked?:boolean Commented Sep 29, 2019 at 10:39
  • i make Checked field optional but it does not work. i want to convert this json data to my class array Roles[]. Commented Sep 29, 2019 at 10:47

2 Answers 2

1

Given the array :

  data = [
    { id: 1, role: "Admin" },
    { id: 2, role: "Manager" },
    { id: 3, role: "User" }
  ];

you can use the following syntax:

let roles: Roles[] = this.data; 

Roles should be defined to:

export class Roles {
    id: number;
    role: string;
    checked?: Boolean;
} 
  1. The property names are case-sensitive
  2. checked is optional now denoted by ?:

EDIT

Since the API returns a response in the format of Roles[] you can simply do:

.subscribe((res: Roles[]) => { 
      let roles = res; 
   }
);
Sign up to request clarification or add additional context in comments.

13 Comments

while assigning to result variable it give me following error "Conversion of type 'string' to type 'Roles[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first."
I've made an edit. Can you try with that? Here's the working stackblitz stackblitz.com/edit/new-del
now i am getting this error "Type 'string' is not assignable to type 'Roles[]'." Actually i am using api to get this data and convert that data into json by using JSON.stringify function.
Then you will need to share that code as well. Share the code of how you are subscribing to this API..
this.http.get('api/user/GetAllRoles'). subscribe(res => { let roles: Roles[] = JSON.stringify(res); console.log("api roles "+ roles) })
|
0

If you have this json-response:

[{"id":1,"role":"Admin"},{"id":2,"role":"Manager"},{"id":3,"role":"User"}]

you should modified your request as:

this.http.get<Role[]>('api/user/GetAllRoles').subscribe((res: HttpResponse) => (roles = res));

This will cast the response to Role[] without the need of JSON.stringify. Before you have to declare roles: Role[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.