0

These are my classes.

Class TypeC {

    int var1;
    HashMap<String,String>var2;
    ArrayList<TypeC> var3;

}

Class TypeB {

TypeC var1;

}

Class TypeA {

    Long var1;
    TypeB var2;
}

I want to create object of TypeC and then convert it into a corresponding JSON object (complex JSON). I tried the following but it doesnt work.

    TypeC obj = new TypeC();
    JSONObject TypeCJSON=new JSONObject(obj);
3
  • Try com.fasterxml.jackson.databind.ObjectMapper Commented Feb 24, 2016 at 13:57
  • I tried looking for it but I dont know how to use it. It will be really nice if you can point me to a good resource with examples for learning this. Commented Feb 24, 2016 at 14:01
  • JSON Jackson library is usually used for converting between POJO (Plain old Java Object) and JSON (and back). Look them up on the internet, please note that you will need to know how to setup your java dev environment for this. Commented Feb 24, 2016 at 14:05

2 Answers 2

1

A full example of data binding using 'com.fasterxml.jackson.databind.ObjectMapper' :

package spring.exos;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

public static void main(String[] args){

    final Computer computer = new Computer();
    computer.setBrand("Toshiba");
    computer.setModel("TSB I7-SSD");
    computer.setSpecs(new Specs(new Integer(256), new Integer(8), new Double(2.4)));

    final ObjectMapper mapper = new ObjectMapper();
    try {
        System.out.println(mapper.writeValueAsString(computer));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

}

public static class Computer{
    private String brand;
    private String model;
    private Specs specs;

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public Specs getSpecs() {
        return specs;
    }
    public void setSpecs(Specs specs) {
        this.specs = specs;
    }
}
public static class Specs {
    private Integer hdd;
    private Integer memory;
    private Double cpu;

    public Specs(Integer hdd, Integer memory, Double cpu) {
        super();
        this.hdd = hdd;
        this.memory = memory;
        this.cpu = cpu;
    }
    public Integer getHdd() {
        return hdd;
    }
    public void setHdd(Integer hdd) {
        this.hdd = hdd;
    }
    public Integer getMemory() {
        return this.memory;
    }
    public void setMemory(Integer memory) {
        this.memory = memory;
    }
    public Double getCpu() {
        return cpu;
    }
    public void setCpu(Double cpu) {
        this.cpu = cpu;
    }
}
}

The output is :

{"brand":"Toshiba","model":"TSB I7-SSD","specs":{"hdd":256,"memory":8,"cpu":2.4}}

You need to have a dependency to:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.1-1</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

0

if you are willing to use another library, using Gson https://github.com/google/gson you just need to do this:

 String json = new Gson().toJson(object);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.