Gson is very usefull for this. easier even. here is my example:
public class Bean {
private String nombre="juan";
private String apellido="machado";
private List<InnerBean> datosCriticos;
class InnerBean
{
private int edad=12;
}
public Bean() {
datosCriticos = new ArrayList<>();
datosCriticos.add(new InnerBean());
}
}
Bean bean = new Bean();
Gson gson = new Gson();
String json =gson.toJson(bean);
out.print(json);
{"nombre":"juan","apellido":"machado","datosCriticos":[{"edad":12}]}
Have to say people if yours vars are empty when using gson it wont build the json for you.Just the
{}