Because of the many advantages of JSON data format, data transfer is now the most popular format for data transmission. So more and more projects use this format to transmit data before and after the platform. Our company background mainly uses the Java platform related technology, therefore in the JSON string format and the Java Object Format transformation becomes the work which we usually need to do. Here's to share some experience with using Json-lib to convert JSON strings to Java objects.
(a) The Java object is converted to a JSON string.
In this case, a specified Java object is converted to the expected string format in more than one encounter. For example, the calendar converts to "Yyyy-mm-dd HH:mm:ss" format string, or date converted to "Yyyy-mm-dd HH:mm:ss" format of the string, and so on. Take a calendar example here to illustrate the conversion method.
Conversion methods
public static String Object2jsonstring (Object object) {
if (object = = null) {
return null;
}
Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.registerjsonvalueprocessor (Calendar.class, New Calendarjsonvalueprocessor ());
JSON jsonobject = Jsonserializer.tojson (object,jsonconfig);
return jsonobject = null? Null:jsonObject.toString ();
}
Define the internal classes used by the transformation
private static class Calendarjsonvalueprocessor implements jsonvalueprocessor{
SimpleDateFormat DateFormat = new SimpleDateFormat (Taskengineconstants.log_date_format);
@Override
Public Object Processarrayvalue (objectobj, Jsonconfig jsonconfig) {
if (obj = = null) {
return null;
}
Return Dateformat.format ((Calendar) obj). GetTime ());
}
@Override
Public Objectprocessobjectvalue (String s, Object obj, Jsonconfig jsonconfig) {
if (obj = = null) {
return null;
}
Return Dateformat.format ((Calendar) obj). GetTime ());
}
}
(ii) The JSON string is converted to a Java object.
When the Java is simpler to convert, it is basically a smooth transition. Here is a special case that we have encountered in the project.
There is a WSDL object hotelbookinginfo, there is a child object Agegroup, it is a Axis2 generated in the WSDL representation of the more than one Java class, in fact, in Java, generally more than one to use enumerations. But the agegroup here is not an enum, but a class with no public constructor, and if you want to instantiate it, you can use the AgeGroup.Factory.fromValue (value) method (Axis2 automatically generated). Because when the json-lib converts a string to a Java object, every time the internal property of the Java object is encountered as a new object, its default public constructor is called and the object is constructed, and the value is populated. Therefore, if you do not do any processing at the time of conversion, the conversion will fail, the report cannot find the Agegourp class public construction method error.
The solution is as follows:
Conversion methods
public static string Jsonstring2object (String jsonstring) {
Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
The key point is below
Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.setnewbeaninstancestrategy (New Hotelnewbeaninstancestrategy ());
Jsonconfig.setrootclass (Hotelbookinginfo.class);
Hotelbookinginfo info = (hotelbookinginfo) Jsonobject.tobean (jsonobject,jsonconfig);
}
Defines the inner class used to modify the default method of constructing objects
public static class Hotelnewbeaninstancestrategy extends Newbeaninstancestrategy {
@Override
Public Object newinstance (CLASSC, Jsonobject Jo) throws Instantiationexception,
Illegalaccessexception,securityexception, Nosuchmethodexception, invocationtargetexception {
Object o;
if (C.equals (Addresstype.class)) {
String t = jo.get ("value"). ToString ();
o = AddressType.Factory.fromValue (t);
}
else if (c.equals (Agegroup.class)) {
String t = jo.get ("value"). ToString ();
o = AgeGroup.Factory.fromValue (t);
}
else {
o = C.newinstance ();
}
return o;
}
}
Also here is a note that the Jsonobject.tobean (Jsonobject, Jsonconfig) method has an overloaded method Jsonobject.tobean (Jsonobject, Hotelbookinginfo.class, Jsonconfig).
If you are using the following code
Jsonobject jsonobject = Jsonobject.fromobject (Dto.getreservationdetails ());
Convert detail to info.
Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.setnewbeaninstancestrategy (Newhotelnewbeaninstancestrategy ());
Hotelbookinginfo info = (hotelbookinginfo) Jsonobject.tobean (Jsonobject, Hotelbookinginfo.class, jsonConfig);
It looks similar to the above, but it throws an exception in this way (Java.lang.ClassCastException:java.lang.Class cannot be cast Tocom.telenav.ws.datatypes.hotel.v10.HotelBookingInfo). Then decompile the code, found that two overloaded methods are written by their implementation, the surface of the results should be the same, but in fact this approach has the problem, remember to have encountered before. So let's pay attention to a similar problem.