Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java JSON question.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java JSON question.

    Hello, I'm using Gson to take JSON from an api and put it into classes; however, I've come across a problem. I'm looking for a way to keep track of what parts belong to what larger parts. As an example:

    Lets say I have a base class that holds all the information, and inside that class Lists are populated using Gson. Those Lists must then be put into other classes, and inside those classes new Lists are formed. It's hard to track which part come from what. Right now I'm using multi-dimensional arrays, but it's getting rather complex to keep this going. Here is an example of the JSON:

    {  
       "item":1,
       "item":2,
       "list":[  
          {  
             "item":1,
             "item":2,
             "list":[  
                {  
                   "item":1,
                   "item":2,
                   "list":[  
                      {  
                         "item":1,
                         "item":2
                      }
                   ]
                }
             ]
          }
       ]
    }

    A big problem is when there are multiple Lists in a class, and a class array is made. A class could have 6 different instances, but only 3 belong to another class instance, and the other 3 to another. It's hard to explain. I need a better way to do this, any suggestions? Thanks, and sorry if this is confusing.


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java JSON question.

    I use these online tools for a head start, google json to java online, they're not great but it's a start & once you already see some of the JSON in JAVA Objects it might save you some time in trying to figure out how to model your JSON in JAVA.
    public class Pojo
    {
        private String item;
     
        private List[] list;
     
        public String getItem ()
        {
            return item;
        }
     
        public void setItem (String item)
        {
            this.item = item;
        }
     
        public List[] getList ()
        {
            return list;
        }
     
        public void setList (List[] list)
        {
            this.list = list;
        }
    }

    Does this code makes sense to you? The model in your JSON is similar to a directory structure with folders containing files and other folders themselves containing containing files. It's a Tree model you're looking for.

    --- Update ---

    You could use something like this also I guess:
    import java.util.ArrayList;
    import java.util.HashMap;
     
    public class ListObject {
        private ListObject parent = null;
        private ArrayList<String> items;
        static HashMap<ListObject, ArrayList<ListObject>> tree  = new HashMap<ListObject, ArrayList<ListObject>>();
     
        public ListObject(ListObject parent) {
        	this.parent = parent;
     
            if (this.parent != null) {
            	if (tree.get(parent) != null) {
            		tree.get(parent).add(this);
            	}
            	else{
            		ArrayList<ListObject> tempList = new ArrayList<ListObject>();
            		tempList.add(this);
            		if (1 != tempList.size())
            			tree.put(this, tempList);
            	}
            }
        } 
     
        public ArrayList<ListObject> getChildren(){
        	return tree.get(this);
        }
     
        public void addItem (String item) {
            if (this.items == null) items = new ArrayList<>();
            this.items.add(item);
        }
     
        public ArrayList<String> getItems () {
            return this.items;
        }
    }
    Last edited by JasonX; September 20th, 2014 at 03:18 PM.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java JSON question.

    The tools found from your suggested google search have really helped. I had no idea about these tools, thanks.

Similar Threads

  1. HTTP post with json data using java
    By v5jagadeesan in forum Java SE APIs
    Replies: 2
    Last Post: January 11th, 2024, 12:48 AM
  2. Java - Curl and Json
    By ste182 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 13th, 2014, 10:58 AM
  3. how to parse JSON using java script
    By mikeclark in forum Member Introductions
    Replies: 1
    Last Post: December 8th, 2012, 11:53 PM
  4. Reg - inserting json data to postgres db using java
    By ramya in forum JDBC & Databases
    Replies: 2
    Last Post: August 27th, 2012, 12:50 PM
  5. Replies: 2
    Last Post: August 27th, 2012, 12:42 PM