• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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

 
Ranch Hand
Posts: 442
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: 3506
496
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: 442
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: 3506
496
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: 3506
496
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:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic