|
Public static void test2 (){
System. out. println ("------------ Object | Map --> JSON string ----------");
Map map = new HashMap ();
Map. put ("name", "json ");
Map. put ("bool", Boolean. TRUE );
Map. put ("int", new Integer (1 ));
Map. put ("arr", new String [] {"a", "B "});
Map. put ("func", "function (I) {return this. arr [I];}");
JSONObject jsonObject1 = JSONObject. fromObject (map );
System. out. println (jsonObject1 );
// Output format: {"func": function (I) {return this. arr [I] ;}, "arr": ["a", "B"], "int": 1, "bool": true, "name ": "json "}
JSONObject jsonObject2 = JSONObject. fromObject (new MyBean ());
System. out. println (jsonObject2 );
// Output format: {"func1": function (I) {return this. options [I];}, "func2": function (I) {return this. options [I];}, "name": "json", "options": ["a", "f"], "pojoId": 1}
}
Public class MyBean {
Private String name = "json ";
Private int pojoId = 1;
Private char [] options = new char [] {'a', 'F '};
Private String func1 = "function (I) {return this. options [I];}";
Private JSONFunction func2 = new JSONFunction (new String [] {"I"}, "return this. options [I];");
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getPojoId (){
Return pojoId;
}
Public void setPojoId (int pojoId ){
This. pojoId = pojoId;
}
Public char [] getOptions (){
Return options;
}
Public void setOptions (char [] options ){
This. options = options;
}
Public String getFunc1 (){
Return func1;
}
Public void setFunc1 (String func1 ){
This. func1 = func1;
}
Public JSONFunction getFunc2 (){
Return func2;
}
Public void setFunc2 (JSONFunction func2 ){
This. func2 = func2;
}
} |