• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

How do I use the data I get in JSON with a get request?

 
Ranch Hand
Posts: 416
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating an application with Spring Boot and Angular.  I was able to do a get request to get data from the database, but it puts it into a JSON format when I get it.   How do I get those individual values and store them into objects?  



That's my typescript code.  The data it sends back looks like this....

{id: 1, schoolName: "Enrico Fermi High School", userName: "enfermi1", password: "dummy", students: Array(2)}

How do I take those individual items, such as schoolName, and store it into a typescript variable?  
 
Marshal
Posts: 3408
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If data is an object, wouldn't it just be something like this?:
 
Nathan Milota
Ranch Hand
Posts: 416
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:If data is an object, wouldn't it just be something like this?:



I get a compiler error when I do it this way, because it says, Property 'schoolName' does not exist on type 'Object'.
 
Ron McLeod
Marshal
Posts: 3408
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can define an interface for the fetched data, then you could use type assertion to override the compiler's understanding of what data is.
 
Ron McLeod
Marshal
Posts: 3408
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the details of what you are doing, but it would be better to have your service return an instance of SchoolRecord so that don;t have to be concerned with it in your business logic.  

Something like this:
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic