Search code examples
javajsonorg.json

From Java object to JsonObject, problem with nested classes


I'm working on a RESTful API project and I'm using JAVA to accomplish the endpoints creation, because the environment where I'm working is an IBM Domino database. I'm using the org.json jar to create the objects and provide all the responses, but now I would modify the project, working directly with Java classes, because it's becoming bigger and bigger... By the way I have problems with nested Java objects.

Basically I have the classes LabelValue, Content and ResultsBlock instantiated in another class that set all the required fields and the generate a JSONObject calling its constructor plus the new object. When I'm doing this I have a Null pointer exception so the system doesn't provide any response. I think that the problem is with the declaration, in the class Resultblock, of the nested Content object. But I don't know how can I manage this kind of situation. Can you help me? When I'm working with easier classes where the attributes are the generical data types and when I create the ArrayList of type Content I've no problem and everything works well.

Thanks in advance!

p.s. I can't use gson library because it creates a java policy problem with IBM Domino server.

public class LabelValue implements java.io.Serializable{
private String label;
private String value;

public void setLabel(String label) {
    this.label = label;
}

public void setValue(String value) {
    this.value = value;
};
}

public class Content implements java.io.Serializable{
private String title;
private String description;
private ArrayList <LabelValue> totals;


public void setTotals(ArrayList<LabelValue> totals) {
    this.totals = totals;
}

public void setTitle(String title) {
    this.title = title;
}

public void setDescription(String description) {
    this.description = description;
}}

public class ResultsBlock implements java.io.Serializable{
private String type;
private Content content;

public void setType(String type) {
    this.type = type;
}

public void setContent(Content content) {
    this.content = content;
}}

in the main  :
{
Content content = new Content();
content.setTitle("title");
content.setDescription("description");
content.setTotals(label);

ResultsBlock result = new ResultsBlock ();
result.setContent(content);
result.setType("result");

return new JSONObject(result);}

this is the expected output of the blockResult class:

"blocks":
{
  "type": "result",
  "content": {
    "title": "title",
    "description": "description",
    "totals": [
      {
        "label": "label",
        "value": value
      }
    ]
 }
  }

Solution

  • If I understand correctly what you need, I would suggest you to use Jackson if you can't use GSON.

    I tried to make a test with the code you provided and, after adding all get methods to your POJO classes, I implemented this example:

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class MainClass {
    
        public static void main(String[] args) throws JsonProcessingException {     
    
            Content content = new Content();
            content.setTitle("title");
            content.setDescription("description");
    
            ResultsBlock result = new ResultsBlock ();
            result.setContent(content);
            result.setType("result");
    
            //  JSONObject(result);
            ObjectMapper mapper = new ObjectMapper(); 
    
            String jsonString = mapper.writeValueAsString(result);
    
            System.out.println(jsonString);
        }
    
    
    }
    

    It prints out the following:

    {
        "type": "result",
        "content": {
            "title": "title",
            "description": "description",
            "totals": null
        }
    }
    

    Now you can access your JSON as you like. Of course I've not considered all fields for simplicity.

    Hope this helps.