i try to convert a class object which are generated via Reflection and convert them to JSON string. following is my methods
public Object creatObjectAsString(String className) {
Object objects = null;
try {
objects = java.lang.reflect.Array.newInstance( Class.forName(className), 1);
//System.out.println(objects.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return objects ;
}
public String convertPlainObjectToJSON( Object obj,boolean isArray){
String jsonString="",tempJSON="";
JSONSerializer serializer = new JSONSerializer();
tempJSON = serializer.serialize(obj);
if(isArray){
jsonString="["+tempJSON+"]";
}else{
jsonString=tempJSON;
}
return jsonString;
}
I have hard coded the following lines since i did not know how to create JSON Array which is not the correct way of programming.
if(isArray){
jsonString="["+tempJSON+"]";
}else{
jsonString=tempJSON;
}
when i printed the convertPlainObjectToJSON result of method i get the following [[null]] which is not expected.
what is the mistake i make.
Please correct me.
[[double square braces, which means theJSONSerializerhas already converted it to anJSONArray. You needn't do it manually. And regd thenull, make sure that yourobjpassed to the methodconvertPlainObjectToJSONis not null.null. You can e.g. create one usingnew Object[0]. @R.J, your comment seems like it would make a good answer, do you want to post it as such?