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

6
  • It's already an array of objects. Commented Jan 3, 2020 at 22:09
  • Does this answer your question? How do I return the response from an asynchronous call? Commented Jan 3, 2020 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: stackoverflow.com/help/how-to-ask Commented Jan 4, 2020 at 0:04
  • What is the error you are getting? Commented Jan 4, 2020 at 0: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 Commented Jan 5, 2020 at 0:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi ORunnals, that makes sense, but I'm getting this error in console : "Uncaught TypeError: Cannot read property 'events' of undefined"
Brilliant! It works... Thank you very much ORunnals. Regards

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.