简体   繁体   English

How to parse a Java object which contains a list of a custom java POJO from REST Api call in component.ts file in angularJs

[英]How to parse a Java object which contains a list of a custom java POJO from REST Api call in component.ts file in angularJs

I am new to angular.我是 angular 的新手。 I stuck to parse the response from the Java REST service to my angular UI.我坚持解析从 Java REST 服务到我的 angular UI 的响应。 I can see the expected list came from REST API call in Network tab of inspector but not able to parse it to my respective component.ts file in angular.我可以看到预期的列表来自检查器的网络选项卡中的 REST API 调用,但无法将其解析为 angular 中我各自的 component.ts 文件。 I am getting Response is undefined in console log of UI.我在 UI 的控制台日志中收到未定义的响应。 Below are the details REST API Response Object以下是详细信息 REST API 响应 Object

public FinalResponse(int statusCode, String statusMessage, Object objects) {
        super();
        this.statusCode = statusCode;
        this.statusMessage = statusMessage;
        this.objects = objects; // This Object holds the Array List of Persons Object 
    }

In My componet.ts file, I used below code在我的 componet.ts 文件中,我使用了以下代码

export class Person {
  constructor( public personId: number, public personName: string,){
  }
}

export class FinalResponse {
 
  constructor( public statusCode: number, public statusMessage: string, 
      public objects:any){

  }
}

export class PersonListComponent implements OnInit {
  
  persons: Person[]
  message: string
  finalResponse: FinalResponse


  constructor(private personDataService: PersonDataService, private router: Router,
    private route: ActivatedRoute) { 
    }

  ngOnInit() {
    this.refreshPersonList();
  }

  refreshPersonList(){
    this.personsDataService.retrieveAllPersons().subscribe(
      response =>{
        console.log("Backend service response"+response)
        this.finalResponse =response;
        this.persons = this.finalResponse.objects
        console.log("Backend service response1 "+this.persons)
        console.log("Backend service response1 "+this.finalResponse.statusCode)
        console.log("Backend service response1 "+this.finalResponse.statusMessage)
        console.log("Backend service response1 "+this.finalResponse.objects)
      }
    ); 
  } 
}

Here I can see the status code & status message perfectly but am not able to see the List of Person object.在这里,我可以完美地看到状态代码和状态消息,但无法看到人员列表 object。 In the UI console log, I can see it is undefined.在 UI 控制台日志中,我可以看到它是未定义的。 Can anyone please help me with this?谁能帮我解决这个问题? Thanks in advance, any advice is highly appreciated.在此先感谢,非常感谢任何建议。

you are assinging wrongly from the response first.您首先从响应中错误地评估。 data keyword is needed for accessing the response data.访问响应数据需要data关键字。 try this: this.persons = this.finalResponse.data.objects试试这个: this.persons = this.finalResponse.data.objects

Here it is:这里是:

this.personsDataService.retrieveAllPersons().subscribe(
      response =>{
        console.log("Backend service response"+response)
        this.finalResponse =response;
        this.persons = this.finalResponse.data.objects
        console.log("Backend service response1 "+this.persons)
        console.log("Backend service response1 "+this.finalResponse.statusCode)
        console.log("Backend service response1 "+this.finalResponse.statusMessage)
        console.log("Backend service response1 "+this.finalResponse.objects)
      }
    ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM