A project requirement requires the return value to be in JSON format, and most of the fields are capitalized, and some are similar to the N_TX format, in the output of such a result encountered a problem, due to the time is tight, directly copy the desired result field to establish the JavaBean class, It was thought that the final call to Json.tostring (obj) would return the result, and it was not expected that the first letter would be automatically lowercase in the return value. View Fastjson source Discovery key in the following paragraph
public static list<fieldinfo> Computegetters (class<?> clazz, map<string, String> AliasMap, Boolean Sorted) {String propertyname; if (Character.isuppercase (C3)) {
if (compatiblewithjavabean) {
propertyname = Introspector.decapitalize (methodname.substring (3));
} else {
propertyname = Character.tolowercase (Methodname.charat (3)) + methodname.substring (4);
}} else if (C3 = = ' _ ') {propertyname = Methodname.substring (4);} else if (C3 = = ' F ') {propertyname = Methodname.substrin g (3); } else {continue;}} In
Compatiblewithjavabean is false when the direct get property has the first letter lowercase, consider
Compatiblewithjavabean initialized to True to discover Public static string decapitalize (string name) { if (name = = Null | | name.length () = = 0) { return name; } If (Name.length () > 1 && Character. Isuppercase(Name.charat (1)) &&Character. Isuppercase (Name.charat (0))) { return name; } char chars[] = Name.tochararray ();Chars[0] = Character. toLowerCase (Chars[0]); return new String (chars); }The first-letter lowercase operation is only allowed in consecutive uppercase letters. Therefore, the method also does not meet the requirements.
Finally, find the following workaround:
1. Change the properties of the bean object directly to public, and the property name to be capitalized, for example {"name": "Nomouse", "Age": 12}, the corresponding Bean is: familiar as public, do not need to define the Get method
public class User {
public String Name;
public int age;
}
2, the first method does not conform to the Java naming convention, you can use the second method to add annotations on attributes:
public class User {
@JSONField (value = "Name")
private String name;
@JSONField (Value = "Age")
private int age;
}
3, their own use of reflection to write Object2json and List2json method, as long as the simple JavaBean class can be processed.
At first, I want to see if the Fastjson serializerfeature serialization property is available, and finally it doesn't fit:
Disablecheckspecialchar: If there is a special word such as double quotation mark in the string property of an object, it will have a backslash transfer character when it is turned into JSON. If you do not need to escape, you can use this property. The default is false quotefieldnames whether double quotation marks are used when the key is ———-output, and the default is true writemapnullvalue--– whether to output a field with a null value, false by default writenullnumberaszero--numeric field if NULL, output is 0, not null writenulllistasempty-–list field if NULL, the output is [], NOT NULL writenullstringasempty-character Type field if NULL, output is "" Instead of NULL Writenullbooleanasfalse–boolean field if NULL, the output is false, not NULL
From for notes (Wiz)
Fastjson The first letter to lowercase when the bean is converted to a string