Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Failed to populate Java object from JSON?

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to poulate a list of javaobjects based on a jsonarray that is returned as response.
The objects are being created and the list is populated, but for some reasson all the properties of the object are null.
Anyone an idea why?
This is my pojo:


The function:
 
Master Rancher
Posts: 4668
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you logged the value of response after your calls to replace?

And then I suggest just trying to get that to work, with no other stuff around it.
A simple test bit of code, with a fixed json String based off that value.

The advantage there is that you can then post it here if it doesn't work and we can see and/or test using the same data and code.
 
Pieter Vandevelde
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did all this before creating this post, I use the same way to populate a different object en there it works fine, while the properties of the javaobject dosn't even have the same names as the properties of the jsonobject.
Now the do...
But for the purpose of the example, I'll post the results here:
ResponseString:

example of the toStringmethod of one of the objects:


But you can't test using the same code, it uses other functions I have ommited and it queries a private endpoint, so you would need an apikey
 
Marshal
Posts: 3182
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what your payload looks like:
The data for the order object is wrapped in an unnamed outer object.

This may not be the best solution, but here's an example of something to work with the data that is being returned:
 
Pieter Vandevelde
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized it was wrapped but I account for that don't I? Otherwise it wouldn't even create the object, but throw an exception.
I also don't understand why I would need the wrapperclass, I use the same way before without any issues
 
Ron McLeod
Marshal
Posts: 3182
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your data is structured as an array of wrapper objects of order object

Your type: List<OrderSummary>> is an array of order objects.
My type: List<WrappedOrder>> is an array of wrapper objects, where the wrapper object contains an order object.
 
Ron McLeod
Marshal
Posts: 3182
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pieter Vandevelde wrote:I also don't understand why I would need the wrapperclass, I use the same way before without any issues


Compare the structure of the JSON payloads between what you had previously and what you have now to see what the differences might be.
 
Ron McLeod
Marshal
Posts: 3182
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pieter Vandevelde wrote:I realized it was wrapped but I account for that don't I? Otherwise it wouldn't even create the object, but throw an exception.


I think GSON silently ignores missing fields and leaves them with their default values, which in your case in null.
 
Pieter Vandevelde
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I see what you ment by it's wrapped, before it was additionally wrapped in an order_History object, but I removed it, for some reasson the same won't work with order.
This is where it did work:

 
Saloon Keeper
Posts: 12269
259
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with your problem, I just wanted to tell you that you should stop manipulating the raw JSON and instead use JSON-B or JAX-B. This will prevent a lot of bugs further down the road:
<br /> I tested it like this: <br />
 
Pieter Vandevelde
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to make an implementation based on your tip, but getting following error:

Although I added both dependencies needed:

When googling the error online I saw that they used:

While I was using

but changing that resulted in:

Any idea how come?
 
Stephan van Hulst
Saloon Keeper
Posts: 12269
259
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic