Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert java object to jason tree or ExtJs tree string

 
Himanshu Bhatnagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have a class called zone , which can have object list of zones themself . so basically a tree kind of object
how i can covert/map this java object to jason string representation of tree .
Is there some api that thake java object and ouput a jason tree string .

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Bhatnagar wrote:Hi,
i have a class called zone , which can have object list of zones themself . so basically a tree kind of object
how i can covert/map this java object to jason string representation of tree .
Is there some api that thake java object and ouput a jason tree string .


please be clear what you are trying to achieve ?
do you want to serialize your object to json ?
 
Himanshu Bhatnagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i wanted the json version of my object , so that i can pass that to extjs store and can get a visual tree
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a library like Jackson to convert your object to a JSON (not "jason") string.
 
Himanshu Bhatnagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am using gson to convert the java objects to json format and i am getting below format :

But i wanted this format for Ext js:


That is the tag called "Children :" should come
 
Ted Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have two choices to get the JSON that you need to work with ExtJS:

  • Change your Java object model to better match what ExtJS expects
  • Write a custom Serializer (or the preferred TypeAdapter) to output the JSON that you need


  • I'd recommend #1 if you can do it, i.e., if you have control over the classes on the server-side. Otherwise the JSON that you need for ExtJS is different enough from your object model that you'll need to write a custom "type adapter" to change how Gson serializes your objects. See http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/TypeAdapter.html for the details.

    ;ted
    --
    Ted M. Young
    Read my new book: The Jackson Cookbook: JSON Recipes in Java
    http://about.me/tedmyoung
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic