0

I'm stuck with getting the JSON below, generated in Spring Boot, into a JavaScript object array. Could you please help to solve?

JSON format from Spring Boot

I'm using XMLHttpRequest. My JS looks like:

let evtObjt = [];

var request = new XMLHttpRequest(); 

request.onreadystatechange = function () {

   if (request.readyState == 4 && request.status == 200){
      var data = JSON.parse(request.response); 
      console.log(data);

      evtObjt = data; 
   }                
}
request.open('GET', '/rest-view/events', true); 
request.send(); 

Regards

Murilo

daveloyall
  • 2,140
  • 21
  • 23
  • It's already an array of objects. – mwilson Jan 03 '20 at 22:09
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Taplar Jan 03 '20 at 22:10
  • 1
    It's convenient for other members if you transcribe your source code into the question instead of posting a picture of it. Please consider the guidelines in asking a question: https://stackoverflow.com/help/how-to-ask – bad_coder Jan 04 '20 at 00:04
  • What is the error you are getting? – Simon Crane Jan 04 '20 at 00:14
  • Hi bad_coder, thank you for your tip but, in this case, I used image for two basic reasons: 1. The image isn't a source code, it's a console print for debugging insight only, as considered in the link you sent me for good/bad practices: "demonstrating rendering bugs, things that are impossible to describe accurately via text." (stackoverflow). 2. The actual source code is in edditable format. Regards – Murilo de Melo Reis Jan 05 '20 at 00:37
  • Hi Simon Crane, thank you for your question. The problem was solved by ORunnals below. I couldn't get JSON into an object array because it was embedded. I had to go all the way through the events "data._embedded.events;" to access it. Now, it's working. Regards – Murilo de Melo Reis Jan 05 '20 at 00:59

1 Answers1

1

If I understand you correctly you might just need to change your assignment to fix it:

evtObjt = data._embedded.events; 

This would assign the events array into the evtObjt. Hopefully that's what you were looking to do.

ORunnals
  • 131
  • 7