• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSON List of Lists not parse properly  RSS feed

 
Atul More
Ranch Hand
Posts: 67
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a List which contains multiple Lists. Each list contains objects of particular type.
The problem is when I am convert the List into Json, it wont show anything.
The List is like:


I have a class which contains this list.


When I am trying to convert this Response class into Json, it created the Json like this:
{"type":"json","message":[[{},{},{}],[{},{}],[{}]]}


It wont convert the lists its contains into Json. The data is there, the list contains the objects but not able to parse it and convert into Json.
I am using Gson for this.

Can anybody tell me, what exactly I am missing.


Thanks,
Atul
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything JSON related in your code.  How are you converting your Java object to JSON?
 
Atul More
Ranch Hand
Posts: 67
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ron,

Thanks for response.

I am converting the Response object into Json.
I set the objects of Component according to type in the respective list and then add that list to message list.
And finally convert the response object into json.

Even I print those individual list objects after coverting them to json.

Might be missing something, just want to understand what it is.

Thanks,
Atul
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66261
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atul More wrote:I am converting the Response object into Json.

Show the code.
 
Atul More
Ranch Hand
Posts: 67
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

The code is like this:










This is a sample code, similar to real. Only thing similar is to set the object into list and conversion.

Thanks,
Atul
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your Response class and a simplified version of your TextProcess class, and I got the JSON output that I expected, so it doesn't look like your problem is with the Response class or with Gson.


 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MOXy (dreadful library, but we have to use it at work) does not look at the actual object types, but at the declared types. In class Response that is Component which has zero properties.

If Gson does the same thing, that would explain the empty objects; [[{},{},{}],[{},{}],[{}]] is a list of lists of empty objects. It would also explain why Ron's example does work - the declared type and actual type are the same.

@Ron: could you try adding an instance of a sub class of Translation that adds some extra properties, and make sure these are set to non-null values? My guess is that these also will not show up.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done some investigating of my own, and it's not the MOXy problem. I have tried the following:

  • Create the sub class and add an instance. The objects got serialized properly.
  • Create a dummy interface for Translation and use that as the inner type (List<List<ITranslation>>*). The objects got serialized properly.
  • Changed the list type to use a wildcard (List<List<? extends ITranslation>>). The objects did not get serialized properly but instead showed up as empty objects.


  • Conclusion: it's the wildcard that's causing the issue.


    * Sorry for the I in ITranslation, but I was too lazy to rename anything or to come up with something better.
     
    Atul More
    Ranch Hand
    Posts: 67
    1
    Java jQuery Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Thanks for response Rob, it helps me alot.
    I am able to solve the issue. The things I have applied are:
    1. Extends the interface Component with Serializable.
    2. Use another json library for conver java to json. The library is Jackson JSON Java Parser.

    Hope it will help someone.


    Thanks,
    Atul
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!